1 // SPDX-License-Identifier: GPL-2.0
3 * IEEE 802.15.4 PAN management
5 * Copyright (C) 2023 Qorvo US, Inc
7 * - David Girault <david.girault@qorvo.com>
8 * - Miquel Raynal <miquel.raynal@bootlin.com>
11 #include <linux/kernel.h>
12 #include <net/cfg802154.h>
13 #include <net/af_ieee802154.h>
15 /* Checks whether a device address matches one from the PAN list.
16 * This helper is meant to be used only during PAN management, when we expect
17 * extended addresses to be used.
19 static bool cfg802154_pan_device_is_matching(struct ieee802154_pan_device
*pan_dev
,
20 struct ieee802154_addr
*ext_dev
)
22 if (!pan_dev
|| !ext_dev
)
25 if (ext_dev
->mode
== IEEE802154_ADDR_SHORT
)
28 return pan_dev
->extended_addr
== ext_dev
->extended_addr
;
31 bool cfg802154_device_is_associated(struct wpan_dev
*wpan_dev
)
35 mutex_lock(&wpan_dev
->association_lock
);
36 is_assoc
= !list_empty(&wpan_dev
->children
) || wpan_dev
->parent
;
37 mutex_unlock(&wpan_dev
->association_lock
);
42 bool cfg802154_device_is_parent(struct wpan_dev
*wpan_dev
,
43 struct ieee802154_addr
*target
)
45 lockdep_assert_held(&wpan_dev
->association_lock
);
47 return cfg802154_pan_device_is_matching(wpan_dev
->parent
, target
);
49 EXPORT_SYMBOL_GPL(cfg802154_device_is_parent
);
51 struct ieee802154_pan_device
*
52 cfg802154_device_is_child(struct wpan_dev
*wpan_dev
,
53 struct ieee802154_addr
*target
)
55 struct ieee802154_pan_device
*child
;
57 lockdep_assert_held(&wpan_dev
->association_lock
);
59 list_for_each_entry(child
, &wpan_dev
->children
, node
)
60 if (cfg802154_pan_device_is_matching(child
, target
))
65 EXPORT_SYMBOL_GPL(cfg802154_device_is_child
);
67 __le16
cfg802154_get_free_short_addr(struct wpan_dev
*wpan_dev
)
69 struct ieee802154_pan_device
*child
;
72 lockdep_assert_held(&wpan_dev
->association_lock
);
75 get_random_bytes(&addr
, 2);
76 if (addr
== cpu_to_le16(IEEE802154_ADDR_SHORT_BROADCAST
) ||
77 addr
== cpu_to_le16(IEEE802154_ADDR_SHORT_UNSPEC
))
80 if (wpan_dev
->short_addr
== addr
)
83 if (wpan_dev
->parent
&& wpan_dev
->parent
->short_addr
== addr
)
86 list_for_each_entry(child
, &wpan_dev
->children
, node
)
87 if (child
->short_addr
== addr
)
95 EXPORT_SYMBOL_GPL(cfg802154_get_free_short_addr
);
97 unsigned int cfg802154_set_max_associations(struct wpan_dev
*wpan_dev
,
100 unsigned int old_max
;
102 lockdep_assert_held(&wpan_dev
->association_lock
);
104 old_max
= wpan_dev
->max_associations
;
105 wpan_dev
->max_associations
= max
;
109 EXPORT_SYMBOL_GPL(cfg802154_set_max_associations
);