projects/linux·blob master · refs

422 lines · 9.0 KiB

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