core.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2007, 2008, 2009 Siemens AG
  4. */
  5. #include <linux/slab.h>
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/device.h>
  9. #include <net/cfg802154.h>
  10. #include <net/rtnetlink.h>
  11. #include "ieee802154.h"
  12. #include "nl802154.h"
  13. #include "sysfs.h"
  14. #include "core.h"
  15. /* name for sysfs, %d is appended */
  16. #define PHY_NAME "phy"
  17. /* RCU-protected (and RTNL for writers) */
  18. LIST_HEAD(cfg802154_rdev_list);
  19. int cfg802154_rdev_list_generation;
  20. struct wpan_phy *wpan_phy_find(const char *str)
  21. {
  22. struct device *dev;
  23. if (WARN_ON(!str))
  24. return NULL;
  25. dev = class_find_device_by_name(&wpan_phy_class, str);
  26. if (!dev)
  27. return NULL;
  28. return container_of(dev, struct wpan_phy, dev);
  29. }
  30. EXPORT_SYMBOL(wpan_phy_find);
  31. struct wpan_phy_iter_data {
  32. int (*fn)(struct wpan_phy *phy, void *data);
  33. void *data;
  34. };
  35. static int wpan_phy_iter(struct device *dev, void *_data)
  36. {
  37. struct wpan_phy_iter_data *wpid = _data;
  38. struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev);
  39. return wpid->fn(phy, wpid->data);
  40. }
  41. int wpan_phy_for_each(int (*fn)(struct wpan_phy *phy, void *data),
  42. void *data)
  43. {
  44. struct wpan_phy_iter_data wpid = {
  45. .fn = fn,
  46. .data = data,
  47. };
  48. return class_for_each_device(&wpan_phy_class, NULL,
  49. &wpid, wpan_phy_iter);
  50. }
  51. EXPORT_SYMBOL(wpan_phy_for_each);
  52. struct cfg802154_registered_device *
  53. cfg802154_rdev_by_wpan_phy_idx(int wpan_phy_idx)
  54. {
  55. struct cfg802154_registered_device *result = NULL, *rdev;
  56. ASSERT_RTNL();
  57. list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
  58. if (rdev->wpan_phy_idx == wpan_phy_idx) {
  59. result = rdev;
  60. break;
  61. }
  62. }
  63. return result;
  64. }
  65. struct wpan_phy *wpan_phy_idx_to_wpan_phy(int wpan_phy_idx)
  66. {
  67. struct cfg802154_registered_device *rdev;
  68. ASSERT_RTNL();
  69. rdev = cfg802154_rdev_by_wpan_phy_idx(wpan_phy_idx);
  70. if (!rdev)
  71. return NULL;
  72. return &rdev->wpan_phy;
  73. }
  74. struct wpan_phy *
  75. wpan_phy_new(const struct cfg802154_ops *ops, size_t priv_size)
  76. {
  77. static atomic_t wpan_phy_counter = ATOMIC_INIT(0);
  78. struct cfg802154_registered_device *rdev;
  79. size_t alloc_size;
  80. alloc_size = sizeof(*rdev) + priv_size;
  81. rdev = kzalloc(alloc_size, GFP_KERNEL);
  82. if (!rdev)
  83. return NULL;
  84. rdev->ops = ops;
  85. rdev->wpan_phy_idx = atomic_inc_return(&wpan_phy_counter);
  86. if (unlikely(rdev->wpan_phy_idx < 0)) {
  87. /* ugh, wrapped! */
  88. atomic_dec(&wpan_phy_counter);
  89. kfree(rdev);
  90. return NULL;
  91. }
  92. /* atomic_inc_return makes it start at 1, make it start at 0 */
  93. rdev->wpan_phy_idx--;
  94. INIT_LIST_HEAD(&rdev->wpan_dev_list);
  95. device_initialize(&rdev->wpan_phy.dev);
  96. dev_set_name(&rdev->wpan_phy.dev, PHY_NAME "%d", rdev->wpan_phy_idx);
  97. rdev->wpan_phy.dev.class = &wpan_phy_class;
  98. rdev->wpan_phy.dev.platform_data = rdev;
  99. wpan_phy_net_set(&rdev->wpan_phy, &init_net);
  100. init_waitqueue_head(&rdev->dev_wait);
  101. init_waitqueue_head(&rdev->wpan_phy.sync_txq);
  102. spin_lock_init(&rdev->wpan_phy.queue_lock);
  103. return &rdev->wpan_phy;
  104. }
  105. EXPORT_SYMBOL(wpan_phy_new);
  106. int wpan_phy_register(struct wpan_phy *phy)
  107. {
  108. struct cfg802154_registered_device *rdev = wpan_phy_to_rdev(phy);
  109. int ret;
  110. rtnl_lock();
  111. ret = device_add(&phy->dev);
  112. if (ret) {
  113. rtnl_unlock();
  114. return ret;
  115. }
  116. list_add_rcu(&rdev->list, &cfg802154_rdev_list);
  117. cfg802154_rdev_list_generation++;
  118. /* TODO phy registered lock */
  119. rtnl_unlock();
  120. /* TODO nl802154 phy notify */
  121. return 0;
  122. }
  123. EXPORT_SYMBOL(wpan_phy_register);
  124. void wpan_phy_unregister(struct wpan_phy *phy)
  125. {
  126. struct cfg802154_registered_device *rdev = wpan_phy_to_rdev(phy);
  127. wait_event(rdev->dev_wait, ({
  128. int __count;
  129. rtnl_lock();
  130. __count = rdev->opencount;
  131. rtnl_unlock();
  132. __count == 0; }));
  133. rtnl_lock();
  134. /* TODO nl802154 phy notify */
  135. /* TODO phy registered lock */
  136. WARN_ON(!list_empty(&rdev->wpan_dev_list));
  137. /* First remove the hardware from everywhere, this makes
  138. * it impossible to find from userspace.
  139. */
  140. list_del_rcu(&rdev->list);
  141. synchronize_rcu();
  142. cfg802154_rdev_list_generation++;
  143. device_del(&phy->dev);
  144. rtnl_unlock();
  145. }
  146. EXPORT_SYMBOL(wpan_phy_unregister);
  147. void wpan_phy_free(struct wpan_phy *phy)
  148. {
  149. put_device(&phy->dev);
  150. }
  151. EXPORT_SYMBOL(wpan_phy_free);
  152. static void cfg802154_free_peer_structures(struct wpan_dev *wpan_dev)
  153. {
  154. struct ieee802154_pan_device *child, *tmp;
  155. mutex_lock(&wpan_dev->association_lock);
  156. kfree(wpan_dev->parent);
  157. wpan_dev->parent = NULL;
  158. list_for_each_entry_safe(child, tmp, &wpan_dev->children, node) {
  159. list_del(&child->node);
  160. kfree(child);
  161. }
  162. wpan_dev->nchildren = 0;
  163. mutex_unlock(&wpan_dev->association_lock);
  164. }
  165. int cfg802154_switch_netns(struct cfg802154_registered_device *rdev,
  166. struct net *net)
  167. {
  168. struct wpan_dev *wpan_dev;
  169. int err = 0;
  170. list_for_each_entry(wpan_dev, &rdev->wpan_dev_list, list) {
  171. if (!wpan_dev->netdev)
  172. continue;
  173. wpan_dev->netdev->netns_local = false;
  174. err = dev_change_net_namespace(wpan_dev->netdev, net, "wpan%d");
  175. if (err)
  176. break;
  177. wpan_dev->netdev->netns_local = true;
  178. }
  179. if (err) {
  180. /* failed -- clean up to old netns */
  181. net = wpan_phy_net(&rdev->wpan_phy);
  182. list_for_each_entry_continue_reverse(wpan_dev,
  183. &rdev->wpan_dev_list,
  184. list) {
  185. if (!wpan_dev->netdev)
  186. continue;
  187. wpan_dev->netdev->netns_local = false;
  188. err = dev_change_net_namespace(wpan_dev->netdev, net,
  189. "wpan%d");
  190. WARN_ON(err);
  191. wpan_dev->netdev->netns_local = true;
  192. }
  193. return err;
  194. }
  195. wpan_phy_net_set(&rdev->wpan_phy, net);
  196. err = device_rename(&rdev->wpan_phy.dev, dev_name(&rdev->wpan_phy.dev));
  197. WARN_ON(err);
  198. return 0;
  199. }
  200. void cfg802154_dev_free(struct cfg802154_registered_device *rdev)
  201. {
  202. kfree(rdev);
  203. }
  204. static void
  205. cfg802154_update_iface_num(struct cfg802154_registered_device *rdev,
  206. int iftype, int num)
  207. {
  208. ASSERT_RTNL();
  209. rdev->num_running_ifaces += num;
  210. }
  211. static int cfg802154_netdev_notifier_call(struct notifier_block *nb,
  212. unsigned long state, void *ptr)
  213. {
  214. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  215. struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  216. struct cfg802154_registered_device *rdev;
  217. if (!wpan_dev)
  218. return NOTIFY_DONE;
  219. rdev = wpan_phy_to_rdev(wpan_dev->wpan_phy);
  220. /* TODO WARN_ON unspec type */
  221. switch (state) {
  222. /* TODO NETDEV_DEVTYPE */
  223. case NETDEV_REGISTER:
  224. dev->netns_local = true;
  225. wpan_dev->identifier = ++rdev->wpan_dev_id;
  226. list_add_rcu(&wpan_dev->list, &rdev->wpan_dev_list);
  227. rdev->devlist_generation++;
  228. mutex_init(&wpan_dev->association_lock);
  229. INIT_LIST_HEAD(&wpan_dev->children);
  230. wpan_dev->max_associations = SZ_16K;
  231. wpan_dev->netdev = dev;
  232. break;
  233. case NETDEV_DOWN:
  234. cfg802154_update_iface_num(rdev, wpan_dev->iftype, -1);
  235. rdev->opencount--;
  236. wake_up(&rdev->dev_wait);
  237. break;
  238. case NETDEV_UP:
  239. cfg802154_update_iface_num(rdev, wpan_dev->iftype, 1);
  240. rdev->opencount++;
  241. break;
  242. case NETDEV_UNREGISTER:
  243. cfg802154_free_peer_structures(wpan_dev);
  244. /* It is possible to get NETDEV_UNREGISTER
  245. * multiple times. To detect that, check
  246. * that the interface is still on the list
  247. * of registered interfaces, and only then
  248. * remove and clean it up.
  249. */
  250. if (!list_empty(&wpan_dev->list)) {
  251. list_del_rcu(&wpan_dev->list);
  252. rdev->devlist_generation++;
  253. }
  254. /* synchronize (so that we won't find this netdev
  255. * from other code any more) and then clear the list
  256. * head so that the above code can safely check for
  257. * !list_empty() to avoid double-cleanup.
  258. */
  259. synchronize_rcu();
  260. INIT_LIST_HEAD(&wpan_dev->list);
  261. break;
  262. default:
  263. return NOTIFY_DONE;
  264. }
  265. return NOTIFY_OK;
  266. }
  267. static struct notifier_block cfg802154_netdev_notifier = {
  268. .notifier_call = cfg802154_netdev_notifier_call,
  269. };
  270. static void __net_exit cfg802154_pernet_exit(struct net *net)
  271. {
  272. struct cfg802154_registered_device *rdev;
  273. rtnl_lock();
  274. list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
  275. if (net_eq(wpan_phy_net(&rdev->wpan_phy), net))
  276. WARN_ON(cfg802154_switch_netns(rdev, &init_net));
  277. }
  278. rtnl_unlock();
  279. }
  280. static struct pernet_operations cfg802154_pernet_ops = {
  281. .exit = cfg802154_pernet_exit,
  282. };
  283. static int __init wpan_phy_class_init(void)
  284. {
  285. int rc;
  286. rc = register_pernet_device(&cfg802154_pernet_ops);
  287. if (rc)
  288. goto err;
  289. rc = wpan_phy_sysfs_init();
  290. if (rc)
  291. goto err_sysfs;
  292. rc = register_netdevice_notifier(&cfg802154_netdev_notifier);
  293. if (rc)
  294. goto err_nl;
  295. rc = ieee802154_nl_init();
  296. if (rc)
  297. goto err_notifier;
  298. rc = nl802154_init();
  299. if (rc)
  300. goto err_ieee802154_nl;
  301. return 0;
  302. err_ieee802154_nl:
  303. ieee802154_nl_exit();
  304. err_notifier:
  305. unregister_netdevice_notifier(&cfg802154_netdev_notifier);
  306. err_nl:
  307. wpan_phy_sysfs_exit();
  308. err_sysfs:
  309. unregister_pernet_device(&cfg802154_pernet_ops);
  310. err:
  311. return rc;
  312. }
  313. subsys_initcall(wpan_phy_class_init);
  314. static void __exit wpan_phy_class_exit(void)
  315. {
  316. nl802154_exit();
  317. ieee802154_nl_exit();
  318. unregister_netdevice_notifier(&cfg802154_netdev_notifier);
  319. wpan_phy_sysfs_exit();
  320. unregister_pernet_device(&cfg802154_pernet_ops);
  321. }
  322. module_exit(wpan_phy_class_exit);
  323. MODULE_LICENSE("GPL v2");
  324. MODULE_DESCRIPTION("IEEE 802.15.4 configuration interface");
  325. MODULE_AUTHOR("Dmitry Eremin-Solenikov");