core.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. return &rdev->wpan_phy;
  102. }
  103. EXPORT_SYMBOL(wpan_phy_new);
  104. int wpan_phy_register(struct wpan_phy *phy)
  105. {
  106. struct cfg802154_registered_device *rdev = wpan_phy_to_rdev(phy);
  107. int ret;
  108. rtnl_lock();
  109. ret = device_add(&phy->dev);
  110. if (ret) {
  111. rtnl_unlock();
  112. return ret;
  113. }
  114. list_add_rcu(&rdev->list, &cfg802154_rdev_list);
  115. cfg802154_rdev_list_generation++;
  116. /* TODO phy registered lock */
  117. rtnl_unlock();
  118. /* TODO nl802154 phy notify */
  119. return 0;
  120. }
  121. EXPORT_SYMBOL(wpan_phy_register);
  122. void wpan_phy_unregister(struct wpan_phy *phy)
  123. {
  124. struct cfg802154_registered_device *rdev = wpan_phy_to_rdev(phy);
  125. wait_event(rdev->dev_wait, ({
  126. int __count;
  127. rtnl_lock();
  128. __count = rdev->opencount;
  129. rtnl_unlock();
  130. __count == 0; }));
  131. rtnl_lock();
  132. /* TODO nl802154 phy notify */
  133. /* TODO phy registered lock */
  134. WARN_ON(!list_empty(&rdev->wpan_dev_list));
  135. /* First remove the hardware from everywhere, this makes
  136. * it impossible to find from userspace.
  137. */
  138. list_del_rcu(&rdev->list);
  139. synchronize_rcu();
  140. cfg802154_rdev_list_generation++;
  141. device_del(&phy->dev);
  142. rtnl_unlock();
  143. }
  144. EXPORT_SYMBOL(wpan_phy_unregister);
  145. void wpan_phy_free(struct wpan_phy *phy)
  146. {
  147. put_device(&phy->dev);
  148. }
  149. EXPORT_SYMBOL(wpan_phy_free);
  150. int cfg802154_switch_netns(struct cfg802154_registered_device *rdev,
  151. struct net *net)
  152. {
  153. struct wpan_dev *wpan_dev;
  154. int err = 0;
  155. list_for_each_entry(wpan_dev, &rdev->wpan_dev_list, list) {
  156. if (!wpan_dev->netdev)
  157. continue;
  158. wpan_dev->netdev->features &= ~NETIF_F_NETNS_LOCAL;
  159. err = dev_change_net_namespace(wpan_dev->netdev, net, "wpan%d");
  160. if (err)
  161. break;
  162. wpan_dev->netdev->features |= NETIF_F_NETNS_LOCAL;
  163. }
  164. if (err) {
  165. /* failed -- clean up to old netns */
  166. net = wpan_phy_net(&rdev->wpan_phy);
  167. list_for_each_entry_continue_reverse(wpan_dev,
  168. &rdev->wpan_dev_list,
  169. list) {
  170. if (!wpan_dev->netdev)
  171. continue;
  172. wpan_dev->netdev->features &= ~NETIF_F_NETNS_LOCAL;
  173. err = dev_change_net_namespace(wpan_dev->netdev, net,
  174. "wpan%d");
  175. WARN_ON(err);
  176. wpan_dev->netdev->features |= NETIF_F_NETNS_LOCAL;
  177. }
  178. return err;
  179. }
  180. wpan_phy_net_set(&rdev->wpan_phy, net);
  181. err = device_rename(&rdev->wpan_phy.dev, dev_name(&rdev->wpan_phy.dev));
  182. WARN_ON(err);
  183. return 0;
  184. }
  185. void cfg802154_dev_free(struct cfg802154_registered_device *rdev)
  186. {
  187. kfree(rdev);
  188. }
  189. static void
  190. cfg802154_update_iface_num(struct cfg802154_registered_device *rdev,
  191. int iftype, int num)
  192. {
  193. ASSERT_RTNL();
  194. rdev->num_running_ifaces += num;
  195. }
  196. static int cfg802154_netdev_notifier_call(struct notifier_block *nb,
  197. unsigned long state, void *ptr)
  198. {
  199. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  200. struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  201. struct cfg802154_registered_device *rdev;
  202. if (!wpan_dev)
  203. return NOTIFY_DONE;
  204. rdev = wpan_phy_to_rdev(wpan_dev->wpan_phy);
  205. /* TODO WARN_ON unspec type */
  206. switch (state) {
  207. /* TODO NETDEV_DEVTYPE */
  208. case NETDEV_REGISTER:
  209. dev->features |= NETIF_F_NETNS_LOCAL;
  210. wpan_dev->identifier = ++rdev->wpan_dev_id;
  211. list_add_rcu(&wpan_dev->list, &rdev->wpan_dev_list);
  212. rdev->devlist_generation++;
  213. wpan_dev->netdev = dev;
  214. break;
  215. case NETDEV_DOWN:
  216. cfg802154_update_iface_num(rdev, wpan_dev->iftype, -1);
  217. rdev->opencount--;
  218. wake_up(&rdev->dev_wait);
  219. break;
  220. case NETDEV_UP:
  221. cfg802154_update_iface_num(rdev, wpan_dev->iftype, 1);
  222. rdev->opencount++;
  223. break;
  224. case NETDEV_UNREGISTER:
  225. /* It is possible to get NETDEV_UNREGISTER
  226. * multiple times. To detect that, check
  227. * that the interface is still on the list
  228. * of registered interfaces, and only then
  229. * remove and clean it up.
  230. */
  231. if (!list_empty(&wpan_dev->list)) {
  232. list_del_rcu(&wpan_dev->list);
  233. rdev->devlist_generation++;
  234. }
  235. /* synchronize (so that we won't find this netdev
  236. * from other code any more) and then clear the list
  237. * head so that the above code can safely check for
  238. * !list_empty() to avoid double-cleanup.
  239. */
  240. synchronize_rcu();
  241. INIT_LIST_HEAD(&wpan_dev->list);
  242. break;
  243. default:
  244. return NOTIFY_DONE;
  245. }
  246. return NOTIFY_OK;
  247. }
  248. static struct notifier_block cfg802154_netdev_notifier = {
  249. .notifier_call = cfg802154_netdev_notifier_call,
  250. };
  251. static void __net_exit cfg802154_pernet_exit(struct net *net)
  252. {
  253. struct cfg802154_registered_device *rdev;
  254. rtnl_lock();
  255. list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
  256. if (net_eq(wpan_phy_net(&rdev->wpan_phy), net))
  257. WARN_ON(cfg802154_switch_netns(rdev, &init_net));
  258. }
  259. rtnl_unlock();
  260. }
  261. static struct pernet_operations cfg802154_pernet_ops = {
  262. .exit = cfg802154_pernet_exit,
  263. };
  264. static int __init wpan_phy_class_init(void)
  265. {
  266. int rc;
  267. rc = register_pernet_device(&cfg802154_pernet_ops);
  268. if (rc)
  269. goto err;
  270. rc = wpan_phy_sysfs_init();
  271. if (rc)
  272. goto err_sysfs;
  273. rc = register_netdevice_notifier(&cfg802154_netdev_notifier);
  274. if (rc)
  275. goto err_nl;
  276. rc = ieee802154_nl_init();
  277. if (rc)
  278. goto err_notifier;
  279. rc = nl802154_init();
  280. if (rc)
  281. goto err_ieee802154_nl;
  282. return 0;
  283. err_ieee802154_nl:
  284. ieee802154_nl_exit();
  285. err_notifier:
  286. unregister_netdevice_notifier(&cfg802154_netdev_notifier);
  287. err_nl:
  288. wpan_phy_sysfs_exit();
  289. err_sysfs:
  290. unregister_pernet_device(&cfg802154_pernet_ops);
  291. err:
  292. return rc;
  293. }
  294. subsys_initcall(wpan_phy_class_init);
  295. static void __exit wpan_phy_class_exit(void)
  296. {
  297. nl802154_exit();
  298. ieee802154_nl_exit();
  299. unregister_netdevice_notifier(&cfg802154_netdev_notifier);
  300. wpan_phy_sysfs_exit();
  301. unregister_pernet_device(&cfg802154_pernet_ops);
  302. }
  303. module_exit(wpan_phy_class_exit);
  304. MODULE_LICENSE("GPL v2");
  305. MODULE_DESCRIPTION("IEEE 802.15.4 configuration interface");
  306. MODULE_AUTHOR("Dmitry Eremin-Solenikov");