pan.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * IEEE 802.15.4 PAN management
  4. *
  5. * Copyright (C) 2023 Qorvo US, Inc
  6. * Authors:
  7. * - David Girault <david.girault@qorvo.com>
  8. * - Miquel Raynal <miquel.raynal@bootlin.com>
  9. */
  10. #include <linux/kernel.h>
  11. #include <net/cfg802154.h>
  12. #include <net/af_ieee802154.h>
  13. /* Checks whether a device address matches one from the PAN list.
  14. * This helper is meant to be used only during PAN management, when we expect
  15. * extended addresses to be used.
  16. */
  17. static bool cfg802154_pan_device_is_matching(struct ieee802154_pan_device *pan_dev,
  18. struct ieee802154_addr *ext_dev)
  19. {
  20. if (!pan_dev || !ext_dev)
  21. return false;
  22. if (ext_dev->mode == IEEE802154_ADDR_SHORT)
  23. return false;
  24. return pan_dev->extended_addr == ext_dev->extended_addr;
  25. }
  26. bool cfg802154_device_is_associated(struct wpan_dev *wpan_dev)
  27. {
  28. bool is_assoc;
  29. mutex_lock(&wpan_dev->association_lock);
  30. is_assoc = !list_empty(&wpan_dev->children) || wpan_dev->parent;
  31. mutex_unlock(&wpan_dev->association_lock);
  32. return is_assoc;
  33. }
  34. bool cfg802154_device_is_parent(struct wpan_dev *wpan_dev,
  35. struct ieee802154_addr *target)
  36. {
  37. lockdep_assert_held(&wpan_dev->association_lock);
  38. return cfg802154_pan_device_is_matching(wpan_dev->parent, target);
  39. }
  40. EXPORT_SYMBOL_GPL(cfg802154_device_is_parent);
  41. struct ieee802154_pan_device *
  42. cfg802154_device_is_child(struct wpan_dev *wpan_dev,
  43. struct ieee802154_addr *target)
  44. {
  45. struct ieee802154_pan_device *child;
  46. lockdep_assert_held(&wpan_dev->association_lock);
  47. list_for_each_entry(child, &wpan_dev->children, node)
  48. if (cfg802154_pan_device_is_matching(child, target))
  49. return child;
  50. return NULL;
  51. }
  52. EXPORT_SYMBOL_GPL(cfg802154_device_is_child);
  53. __le16 cfg802154_get_free_short_addr(struct wpan_dev *wpan_dev)
  54. {
  55. struct ieee802154_pan_device *child;
  56. __le16 addr;
  57. lockdep_assert_held(&wpan_dev->association_lock);
  58. do {
  59. get_random_bytes(&addr, 2);
  60. if (addr == cpu_to_le16(IEEE802154_ADDR_SHORT_BROADCAST) ||
  61. addr == cpu_to_le16(IEEE802154_ADDR_SHORT_UNSPEC))
  62. continue;
  63. if (wpan_dev->short_addr == addr)
  64. continue;
  65. if (wpan_dev->parent && wpan_dev->parent->short_addr == addr)
  66. continue;
  67. list_for_each_entry(child, &wpan_dev->children, node)
  68. if (child->short_addr == addr)
  69. continue;
  70. break;
  71. } while (1);
  72. return addr;
  73. }
  74. EXPORT_SYMBOL_GPL(cfg802154_get_free_short_addr);
  75. unsigned int cfg802154_set_max_associations(struct wpan_dev *wpan_dev,
  76. unsigned int max)
  77. {
  78. unsigned int old_max;
  79. lockdep_assert_held(&wpan_dev->association_lock);
  80. old_max = wpan_dev->max_associations;
  81. wpan_dev->max_associations = max;
  82. return old_max;
  83. }
  84. EXPORT_SYMBOL_GPL(cfg802154_set_max_associations);