1 // SPDX-License-Identifier: GPL-2.0 1 // SPDX-License-Identifier: GPL-2.0 2 /* 2 /* 3 * IEEE 802.15.4 PAN management 3 * IEEE 802.15.4 PAN management 4 * 4 * 5 * Copyright (C) 2023 Qorvo US, Inc 5 * Copyright (C) 2023 Qorvo US, Inc 6 * Authors: 6 * Authors: 7 * - David Girault <david.girault@qorvo.com> 7 * - David Girault <david.girault@qorvo.com> 8 * - Miquel Raynal <miquel.raynal@bootlin.co 8 * - Miquel Raynal <miquel.raynal@bootlin.com> 9 */ 9 */ 10 10 11 #include <linux/kernel.h> 11 #include <linux/kernel.h> 12 #include <net/cfg802154.h> 12 #include <net/cfg802154.h> 13 #include <net/af_ieee802154.h> 13 #include <net/af_ieee802154.h> 14 14 15 /* Checks whether a device address matches one 15 /* Checks whether a device address matches one from the PAN list. 16 * This helper is meant to be used only during 16 * This helper is meant to be used only during PAN management, when we expect 17 * extended addresses to be used. 17 * extended addresses to be used. 18 */ 18 */ 19 static bool cfg802154_pan_device_is_matching(s 19 static bool cfg802154_pan_device_is_matching(struct ieee802154_pan_device *pan_dev, 20 s 20 struct ieee802154_addr *ext_dev) 21 { 21 { 22 if (!pan_dev || !ext_dev) 22 if (!pan_dev || !ext_dev) 23 return false; 23 return false; 24 24 25 if (ext_dev->mode == IEEE802154_ADDR_S 25 if (ext_dev->mode == IEEE802154_ADDR_SHORT) 26 return false; 26 return false; 27 27 28 return pan_dev->extended_addr == ext_d 28 return pan_dev->extended_addr == ext_dev->extended_addr; 29 } 29 } 30 30 31 bool cfg802154_device_is_associated(struct wpa 31 bool cfg802154_device_is_associated(struct wpan_dev *wpan_dev) 32 { 32 { 33 bool is_assoc; 33 bool is_assoc; 34 34 35 mutex_lock(&wpan_dev->association_lock 35 mutex_lock(&wpan_dev->association_lock); 36 is_assoc = !list_empty(&wpan_dev->chil 36 is_assoc = !list_empty(&wpan_dev->children) || wpan_dev->parent; 37 mutex_unlock(&wpan_dev->association_lo 37 mutex_unlock(&wpan_dev->association_lock); 38 38 39 return is_assoc; 39 return is_assoc; 40 } 40 } 41 41 42 bool cfg802154_device_is_parent(struct wpan_de 42 bool cfg802154_device_is_parent(struct wpan_dev *wpan_dev, 43 struct ieee802 43 struct ieee802154_addr *target) 44 { 44 { 45 lockdep_assert_held(&wpan_dev->associa 45 lockdep_assert_held(&wpan_dev->association_lock); 46 46 47 return cfg802154_pan_device_is_matchin 47 return cfg802154_pan_device_is_matching(wpan_dev->parent, target); 48 } 48 } 49 EXPORT_SYMBOL_GPL(cfg802154_device_is_parent); 49 EXPORT_SYMBOL_GPL(cfg802154_device_is_parent); 50 50 51 struct ieee802154_pan_device * 51 struct ieee802154_pan_device * 52 cfg802154_device_is_child(struct wpan_dev *wpa 52 cfg802154_device_is_child(struct wpan_dev *wpan_dev, 53 struct ieee802154_ad 53 struct ieee802154_addr *target) 54 { 54 { 55 struct ieee802154_pan_device *child; 55 struct ieee802154_pan_device *child; 56 56 57 lockdep_assert_held(&wpan_dev->associa 57 lockdep_assert_held(&wpan_dev->association_lock); 58 58 59 list_for_each_entry(child, &wpan_dev-> 59 list_for_each_entry(child, &wpan_dev->children, node) 60 if (cfg802154_pan_device_is_ma 60 if (cfg802154_pan_device_is_matching(child, target)) 61 return child; 61 return child; 62 62 63 return NULL; 63 return NULL; 64 } 64 } 65 EXPORT_SYMBOL_GPL(cfg802154_device_is_child); 65 EXPORT_SYMBOL_GPL(cfg802154_device_is_child); 66 66 67 __le16 cfg802154_get_free_short_addr(struct wp 67 __le16 cfg802154_get_free_short_addr(struct wpan_dev *wpan_dev) 68 { 68 { 69 struct ieee802154_pan_device *child; 69 struct ieee802154_pan_device *child; 70 __le16 addr; 70 __le16 addr; 71 71 72 lockdep_assert_held(&wpan_dev->associa 72 lockdep_assert_held(&wpan_dev->association_lock); 73 73 74 do { 74 do { 75 get_random_bytes(&addr, 2); 75 get_random_bytes(&addr, 2); 76 if (addr == cpu_to_le16(IEEE80 76 if (addr == cpu_to_le16(IEEE802154_ADDR_SHORT_BROADCAST) || 77 addr == cpu_to_le16(IEEE80 77 addr == cpu_to_le16(IEEE802154_ADDR_SHORT_UNSPEC)) 78 continue; 78 continue; 79 79 80 if (wpan_dev->short_addr == ad 80 if (wpan_dev->short_addr == addr) 81 continue; 81 continue; 82 82 83 if (wpan_dev->parent && wpan_d 83 if (wpan_dev->parent && wpan_dev->parent->short_addr == addr) 84 continue; 84 continue; 85 85 86 list_for_each_entry(child, &wp 86 list_for_each_entry(child, &wpan_dev->children, node) 87 if (child->short_addr 87 if (child->short_addr == addr) 88 continue; 88 continue; 89 89 90 break; 90 break; 91 } while (1); 91 } while (1); 92 92 93 return addr; 93 return addr; 94 } 94 } 95 EXPORT_SYMBOL_GPL(cfg802154_get_free_short_add 95 EXPORT_SYMBOL_GPL(cfg802154_get_free_short_addr); 96 96 97 unsigned int cfg802154_set_max_associations(st 97 unsigned int cfg802154_set_max_associations(struct wpan_dev *wpan_dev, 98 un 98 unsigned int max) 99 { 99 { 100 unsigned int old_max; 100 unsigned int old_max; 101 101 102 lockdep_assert_held(&wpan_dev->associa 102 lockdep_assert_held(&wpan_dev->association_lock); 103 103 104 old_max = wpan_dev->max_associations; 104 old_max = wpan_dev->max_associations; 105 wpan_dev->max_associations = max; 105 wpan_dev->max_associations = max; 106 106 107 return old_max; 107 return old_max; 108 } 108 } 109 EXPORT_SYMBOL_GPL(cfg802154_set_max_associatio 109 EXPORT_SYMBOL_GPL(cfg802154_set_max_associations); 110 110
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.