diff mbox series

[wpan-next,06/11] mac802154: Handle disassociations

Message ID 20230601154817.754519-7-miquel.raynal@bootlin.com (mailing list archive)
State Changes Requested
Headers show
Series ieee802154: Associations between devices | expand

Commit Message

Miquel Raynal June 1, 2023, 3:48 p.m. UTC
Devices may decide to disassociate from their coordinator for different
reasons (device turning off, coordinator signal strength too low, etc),
the MAC layer just has to send a disassociation notification.

If the ack of the disassociation notification is not received, the
device may consider itself disassociated anyway.

Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
---
 net/ieee802154/pan.c         |   2 +
 net/mac802154/cfg.c          | 102 +++++++++++++++++++++++++++++++++++
 net/mac802154/ieee802154_i.h |   4 ++
 net/mac802154/scan.c         |  60 +++++++++++++++++++++
 4 files changed, 168 insertions(+)

Comments

Alexander Aring June 3, 2023, 11:30 a.m. UTC | #1
Hi,

On Thu, Jun 1, 2023 at 11:50 AM Miquel Raynal <miquel.raynal@bootlin.com> wrote:
>
> Devices may decide to disassociate from their coordinator for different
> reasons (device turning off, coordinator signal strength too low, etc),
> the MAC layer just has to send a disassociation notification.
>
> If the ack of the disassociation notification is not received, the
> device may consider itself disassociated anyway.
>
> Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
> ---
>  net/ieee802154/pan.c         |   2 +
>  net/mac802154/cfg.c          | 102 +++++++++++++++++++++++++++++++++++
>  net/mac802154/ieee802154_i.h |   4 ++
>  net/mac802154/scan.c         |  60 +++++++++++++++++++++
>  4 files changed, 168 insertions(+)
>
> diff --git a/net/ieee802154/pan.c b/net/ieee802154/pan.c
> index e2a12a42ba2b..477e8dad0cf0 100644
> --- a/net/ieee802154/pan.c
> +++ b/net/ieee802154/pan.c
> @@ -49,6 +49,7 @@ bool cfg802154_device_is_parent(struct wpan_dev *wpan_dev,
>
>         return false;
>  }
> +EXPORT_SYMBOL_GPL(cfg802154_device_is_parent);
>
>  struct ieee802154_pan_device *
>  cfg802154_device_is_child(struct wpan_dev *wpan_dev,
> @@ -64,3 +65,4 @@ cfg802154_device_is_child(struct wpan_dev *wpan_dev,
>
>         return NULL;
>  }
> +EXPORT_SYMBOL_GPL(cfg802154_device_is_child);
> diff --git a/net/mac802154/cfg.c b/net/mac802154/cfg.c
> index 89112d2bcee7..c27c05e825ff 100644
> --- a/net/mac802154/cfg.c
> +++ b/net/mac802154/cfg.c
> @@ -386,6 +386,107 @@ static int mac802154_associate(struct wpan_phy *wpan_phy,
>         return ret;
>  }
>
> +static int mac802154_disassociate_from_parent(struct wpan_phy *wpan_phy,
> +                                             struct wpan_dev *wpan_dev)
> +{
> +       struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
> +       struct ieee802154_pan_device *child, *tmp;
> +       struct ieee802154_sub_if_data *sdata;
> +       u64 eaddr;
> +       int ret;
> +
> +       sdata = IEEE802154_WPAN_DEV_TO_SUB_IF(wpan_dev);
> +
> +       /* Start by disassociating all the children and preventing new ones to
> +        * attempt associations.
> +        */
> +       list_for_each_entry_safe(child, tmp, &wpan_dev->children, node) {
> +               ret = mac802154_send_disassociation_notif(sdata, child,
> +                                                         IEEE802154_COORD_WISHES_DEVICE_TO_LEAVE);
> +               if (ret) {
> +                       eaddr = swab64((__force u64)child->extended_addr);

Does this pass sparse? I think this needs to be le64_to_cpu()?

- Alex
Miquel Raynal Aug. 21, 2023, 9:02 a.m. UTC | #2
Hi Alexander,

aahringo@redhat.com wrote on Sat, 3 Jun 2023 07:30:05 -0400:

> Hi,
> 
> On Thu, Jun 1, 2023 at 11:50 AM Miquel Raynal <miquel.raynal@bootlin.com> wrote:
> >
> > Devices may decide to disassociate from their coordinator for different
> > reasons (device turning off, coordinator signal strength too low, etc),
> > the MAC layer just has to send a disassociation notification.
> >
> > If the ack of the disassociation notification is not received, the
> > device may consider itself disassociated anyway.
> >
> > Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
> > ---
> >  net/ieee802154/pan.c         |   2 +
> >  net/mac802154/cfg.c          | 102 +++++++++++++++++++++++++++++++++++
> >  net/mac802154/ieee802154_i.h |   4 ++
> >  net/mac802154/scan.c         |  60 +++++++++++++++++++++
> >  4 files changed, 168 insertions(+)
> >
> > diff --git a/net/ieee802154/pan.c b/net/ieee802154/pan.c
> > index e2a12a42ba2b..477e8dad0cf0 100644
> > --- a/net/ieee802154/pan.c
> > +++ b/net/ieee802154/pan.c
> > @@ -49,6 +49,7 @@ bool cfg802154_device_is_parent(struct wpan_dev *wpan_dev,
> >
> >         return false;
> >  }
> > +EXPORT_SYMBOL_GPL(cfg802154_device_is_parent);
> >
> >  struct ieee802154_pan_device *
> >  cfg802154_device_is_child(struct wpan_dev *wpan_dev,
> > @@ -64,3 +65,4 @@ cfg802154_device_is_child(struct wpan_dev *wpan_dev,
> >
> >         return NULL;
> >  }
> > +EXPORT_SYMBOL_GPL(cfg802154_device_is_child);
> > diff --git a/net/mac802154/cfg.c b/net/mac802154/cfg.c
> > index 89112d2bcee7..c27c05e825ff 100644
> > --- a/net/mac802154/cfg.c
> > +++ b/net/mac802154/cfg.c
> > @@ -386,6 +386,107 @@ static int mac802154_associate(struct wpan_phy *wpan_phy,
> >         return ret;
> >  }
> >
> > +static int mac802154_disassociate_from_parent(struct wpan_phy *wpan_phy,
> > +                                             struct wpan_dev *wpan_dev)
> > +{
> > +       struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
> > +       struct ieee802154_pan_device *child, *tmp;
> > +       struct ieee802154_sub_if_data *sdata;
> > +       u64 eaddr;
> > +       int ret;
> > +
> > +       sdata = IEEE802154_WPAN_DEV_TO_SUB_IF(wpan_dev);
> > +
> > +       /* Start by disassociating all the children and preventing new ones to
> > +        * attempt associations.
> > +        */
> > +       list_for_each_entry_safe(child, tmp, &wpan_dev->children, node) {
> > +               ret = mac802154_send_disassociation_notif(sdata, child,
> > +                                                         IEEE802154_COORD_WISHES_DEVICE_TO_LEAVE);
> > +               if (ret) {
> > +                       eaddr = swab64((__force u64)child->extended_addr);  
> 
> Does this pass sparse? I think this needs to be le64_to_cpu()?

I never feel comfortable with sparse given the dozens (hundreds) of
lines it outputs, but I think yes, parse does not seem to complain. To
be honest I think we should keep it this way just because I copy-pasted
it from other locations in the core:

$ git grep -c "swab64((__force u64)" -- net/ieee802154/ net/mac802154/
net/ieee802154/nl-mac.c:1
net/mac802154/cfg.c:4
net/mac802154/llsec.c:1
net/mac802154/rx.c:2
net/mac802154/scan.c:6

So if we ever want to change that, we could easily find them all and
replace them all in one go?

Thanks,
Miquèl
diff mbox series

Patch

diff --git a/net/ieee802154/pan.c b/net/ieee802154/pan.c
index e2a12a42ba2b..477e8dad0cf0 100644
--- a/net/ieee802154/pan.c
+++ b/net/ieee802154/pan.c
@@ -49,6 +49,7 @@  bool cfg802154_device_is_parent(struct wpan_dev *wpan_dev,
 
 	return false;
 }
+EXPORT_SYMBOL_GPL(cfg802154_device_is_parent);
 
 struct ieee802154_pan_device *
 cfg802154_device_is_child(struct wpan_dev *wpan_dev,
@@ -64,3 +65,4 @@  cfg802154_device_is_child(struct wpan_dev *wpan_dev,
 
 	return NULL;
 }
+EXPORT_SYMBOL_GPL(cfg802154_device_is_child);
diff --git a/net/mac802154/cfg.c b/net/mac802154/cfg.c
index 89112d2bcee7..c27c05e825ff 100644
--- a/net/mac802154/cfg.c
+++ b/net/mac802154/cfg.c
@@ -386,6 +386,107 @@  static int mac802154_associate(struct wpan_phy *wpan_phy,
 	return ret;
 }
 
+static int mac802154_disassociate_from_parent(struct wpan_phy *wpan_phy,
+					      struct wpan_dev *wpan_dev)
+{
+	struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
+	struct ieee802154_pan_device *child, *tmp;
+	struct ieee802154_sub_if_data *sdata;
+	u64 eaddr;
+	int ret;
+
+	sdata = IEEE802154_WPAN_DEV_TO_SUB_IF(wpan_dev);
+
+	/* Start by disassociating all the children and preventing new ones to
+	 * attempt associations.
+	 */
+	list_for_each_entry_safe(child, tmp, &wpan_dev->children, node) {
+		ret = mac802154_send_disassociation_notif(sdata, child,
+							  IEEE802154_COORD_WISHES_DEVICE_TO_LEAVE);
+		if (ret) {
+			eaddr = swab64((__force u64)child->extended_addr);
+			dev_err(&sdata->dev->dev,
+				"Disassociation with %8phC may have failed (%d)\n",
+				&eaddr, ret);
+		}
+
+		list_del(&child->node);
+	}
+
+	ret = mac802154_send_disassociation_notif(sdata, wpan_dev->parent,
+						  IEEE802154_DEVICE_WISHES_TO_LEAVE);
+	if (ret) {
+		eaddr = swab64((__force u64)wpan_dev->parent->extended_addr);
+		dev_err(&sdata->dev->dev,
+			"Disassociation from %8phC may have failed (%d)\n",
+			&eaddr, ret);
+	}
+
+	ret = 0;
+
+	kfree(wpan_dev->parent);
+	wpan_dev->parent = NULL;
+	wpan_dev->association_generation++;
+	wpan_dev->pan_id = cpu_to_le16(IEEE802154_PAN_ID_BROADCAST);
+	wpan_dev->short_addr = cpu_to_le16(IEEE802154_ADDR_SHORT_BROADCAST);
+
+	if (local->hw.flags & IEEE802154_HW_AFILT) {
+		ret = drv_set_pan_id(local, wpan_dev->pan_id);
+		if (ret < 0)
+			return ret;
+
+		ret = drv_set_short_addr(local, wpan_dev->short_addr);
+		if (ret < 0)
+			return ret;
+	}
+
+	return 0;
+}
+
+static int mac802154_disassociate_child(struct wpan_phy *wpan_phy,
+					struct wpan_dev *wpan_dev,
+					struct ieee802154_pan_device *child)
+{
+	struct ieee802154_sub_if_data *sdata;
+	int ret;
+
+	sdata = IEEE802154_WPAN_DEV_TO_SUB_IF(wpan_dev);
+
+	ret = mac802154_send_disassociation_notif(sdata, child,
+						  IEEE802154_COORD_WISHES_DEVICE_TO_LEAVE);
+	if (ret)
+		return ret;
+
+	list_del(&child->node);
+	wpan_dev->association_generation++;
+	kfree(child);
+
+	return 0;
+}
+
+static int mac802154_disassociate(struct wpan_phy *wpan_phy,
+				  struct wpan_dev *wpan_dev,
+				  struct ieee802154_addr *target)
+{
+	u64 teaddr = swab64((__force u64)target->extended_addr);
+	struct ieee802154_pan_device *pan_device;
+
+	ASSERT_RTNL();
+
+	if (cfg802154_device_is_parent(wpan_dev, target))
+		return mac802154_disassociate_from_parent(wpan_phy, wpan_dev);
+
+	pan_device = cfg802154_device_is_child(wpan_dev, target);
+	if (pan_device)
+		return mac802154_disassociate_child(wpan_phy, wpan_dev,
+						    pan_device);
+
+	dev_err(&wpan_dev->netdev->dev,
+		"Device %8phC is not associated with us\n", &teaddr);
+
+	return -EINVAL;
+}
+
 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
 static void
 ieee802154_get_llsec_table(struct wpan_phy *wpan_phy,
@@ -598,6 +699,7 @@  const struct cfg802154_ops mac802154_config_ops = {
 	.send_beacons = mac802154_send_beacons,
 	.stop_beacons = mac802154_stop_beacons,
 	.associate = mac802154_associate,
+	.disassociate = mac802154_disassociate,
 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
 	.get_llsec_table = ieee802154_get_llsec_table,
 	.lock_llsec_table = ieee802154_lock_llsec_table,
diff --git a/net/mac802154/ieee802154_i.h b/net/mac802154/ieee802154_i.h
index fff67676b400..92252f86c69c 100644
--- a/net/mac802154/ieee802154_i.h
+++ b/net/mac802154/ieee802154_i.h
@@ -315,6 +315,10 @@  static inline bool mac802154_is_associating(struct ieee802154_local *local)
 	return test_bit(IEEE802154_IS_ASSOCIATING, &local->ongoing);
 }
 
+int mac802154_send_disassociation_notif(struct ieee802154_sub_if_data *sdata,
+					struct ieee802154_pan_device *target,
+					u8 reason);
+
 /* interface handling */
 int ieee802154_iface_init(void);
 void ieee802154_iface_exit(void);
diff --git a/net/mac802154/scan.c b/net/mac802154/scan.c
index 5dd50e1ce329..e2f2e1235ec6 100644
--- a/net/mac802154/scan.c
+++ b/net/mac802154/scan.c
@@ -637,3 +637,63 @@  int mac802154_process_association_resp(struct ieee802154_sub_if_data *sdata,
 
 	return 0;
 }
+
+int mac802154_send_disassociation_notif(struct ieee802154_sub_if_data *sdata,
+					struct ieee802154_pan_device *target,
+					u8 reason)
+{
+	struct ieee802154_disassociation_notif_frame frame = {};
+	u64 teaddr = swab64((__force u64)target->extended_addr);
+	struct ieee802154_local *local = sdata->local;
+	struct wpan_dev *wpan_dev = &sdata->wpan_dev;
+	struct sk_buff *skb;
+	int ret;
+
+	frame.mhr.fc.type = IEEE802154_FC_TYPE_MAC_CMD;
+	frame.mhr.fc.security_enabled = 0;
+	frame.mhr.fc.frame_pending = 0;
+	frame.mhr.fc.ack_request = 1;
+	frame.mhr.fc.intra_pan = 1;
+	frame.mhr.fc.dest_addr_mode = (target->mode == IEEE802154_ADDR_LONG) ?
+		IEEE802154_EXTENDED_ADDRESSING : IEEE802154_SHORT_ADDRESSING;
+	frame.mhr.fc.version = IEEE802154_2003_STD;
+	frame.mhr.fc.source_addr_mode = IEEE802154_EXTENDED_ADDRESSING;
+	frame.mhr.source.mode = IEEE802154_ADDR_LONG;
+	frame.mhr.source.pan_id = wpan_dev->pan_id;
+	frame.mhr.source.extended_addr = wpan_dev->extended_addr;
+	frame.mhr.dest.mode = target->mode;
+	frame.mhr.dest.pan_id = wpan_dev->pan_id;
+	if (target->mode == IEEE802154_ADDR_LONG)
+		frame.mhr.dest.extended_addr = target->extended_addr;
+	else
+		frame.mhr.dest.short_addr = target->short_addr;
+	frame.mhr.seq = atomic_inc_return(&wpan_dev->dsn) & 0xFF;
+	frame.mac_pl.cmd_id = IEEE802154_CMD_DISASSOCIATION_NOTIFY;
+	frame.disassoc_pl = reason;
+
+	skb = alloc_skb(IEEE802154_MAC_CMD_SKB_SZ + sizeof(frame.disassoc_pl),
+			GFP_KERNEL);
+	if (!skb)
+		return -ENOBUFS;
+
+	skb->dev = sdata->dev;
+
+	ret = ieee802154_mac_cmd_push(skb, &frame, &frame.disassoc_pl,
+				      sizeof(frame.disassoc_pl));
+	if (ret) {
+		kfree_skb(skb);
+		return ret;
+	}
+
+	ret = ieee802154_mlme_tx_one_locked(local, sdata, skb);
+	if (ret) {
+		dev_warn(&sdata->dev->dev,
+			 "No DISASSOC ACK received from %8phC\n", &teaddr);
+		if (ret > 0)
+			ret = (ret == IEEE802154_NO_ACK) ? -EREMOTEIO : -EIO;
+		return ret;
+	}
+
+	dev_dbg(&sdata->dev->dev, "DISASSOC ACK received from %8phC\n", &teaddr);
+	return 0;
+}