]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/wireless/core.c
mac80211: make rx radiotap header more flexible
[mirror_ubuntu-bionic-kernel.git] / net / wireless / core.c
CommitLineData
704232c2
JB
1/*
2 * This is the linux wireless configuration interface.
3 *
4 * Copyright 2006, 2007 Johannes Berg <johannes@sipsolutions.net>
5 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
10#include <linux/mutex.h>
11#include <linux/list.h>
12#include <linux/nl80211.h>
13#include <linux/debugfs.h>
14#include <linux/notifier.h>
15#include <linux/device.h>
16#include <net/genetlink.h>
17#include <net/cfg80211.h>
18#include <net/wireless.h>
55682965 19#include "nl80211.h"
704232c2
JB
20#include "core.h"
21#include "sysfs.h"
22
23/* name for sysfs, %d is appended */
24#define PHY_NAME "phy"
25
26MODULE_AUTHOR("Johannes Berg");
27MODULE_LICENSE("GPL");
28MODULE_DESCRIPTION("wireless configuration support");
29
30/* RCU might be appropriate here since we usually
31 * only read the list, and that can happen quite
32 * often because we need to do it for each command */
33LIST_HEAD(cfg80211_drv_list);
34DEFINE_MUTEX(cfg80211_drv_mutex);
35static int wiphy_counter;
36
37/* for debugfs */
38static struct dentry *ieee80211_debugfs_dir;
39
55682965
JB
40/* requires cfg80211_drv_mutex to be held! */
41static struct cfg80211_registered_device *cfg80211_drv_by_wiphy(int wiphy)
42{
43 struct cfg80211_registered_device *result = NULL, *drv;
44
45 list_for_each_entry(drv, &cfg80211_drv_list, list) {
46 if (drv->idx == wiphy) {
47 result = drv;
48 break;
49 }
50 }
51
52 return result;
53}
54
55/* requires cfg80211_drv_mutex to be held! */
56static struct cfg80211_registered_device *
57__cfg80211_drv_from_info(struct genl_info *info)
58{
59 int ifindex;
60 struct cfg80211_registered_device *bywiphy = NULL, *byifidx = NULL;
61 struct net_device *dev;
62 int err = -EINVAL;
63
64 if (info->attrs[NL80211_ATTR_WIPHY]) {
65 bywiphy = cfg80211_drv_by_wiphy(
66 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY]));
67 err = -ENODEV;
68 }
69
70 if (info->attrs[NL80211_ATTR_IFINDEX]) {
71 ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
72 dev = dev_get_by_index(&init_net, ifindex);
73 if (dev) {
74 if (dev->ieee80211_ptr)
75 byifidx =
76 wiphy_to_dev(dev->ieee80211_ptr->wiphy);
77 dev_put(dev);
78 }
79 err = -ENODEV;
80 }
81
82 if (bywiphy && byifidx) {
83 if (bywiphy != byifidx)
84 return ERR_PTR(-EINVAL);
85 else
86 return bywiphy; /* == byifidx */
87 }
88 if (bywiphy)
89 return bywiphy;
90
91 if (byifidx)
92 return byifidx;
93
94 return ERR_PTR(err);
95}
96
97struct cfg80211_registered_device *
98cfg80211_get_dev_from_info(struct genl_info *info)
99{
100 struct cfg80211_registered_device *drv;
101
102 mutex_lock(&cfg80211_drv_mutex);
103 drv = __cfg80211_drv_from_info(info);
104
105 /* if it is not an error we grab the lock on
106 * it to assure it won't be going away while
107 * we operate on it */
108 if (!IS_ERR(drv))
109 mutex_lock(&drv->mtx);
110
111 mutex_unlock(&cfg80211_drv_mutex);
112
113 return drv;
114}
115
116struct cfg80211_registered_device *
117cfg80211_get_dev_from_ifindex(int ifindex)
118{
119 struct cfg80211_registered_device *drv = ERR_PTR(-ENODEV);
120 struct net_device *dev;
121
122 mutex_lock(&cfg80211_drv_mutex);
123 dev = dev_get_by_index(&init_net, ifindex);
124 if (!dev)
125 goto out;
126 if (dev->ieee80211_ptr) {
127 drv = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
128 mutex_lock(&drv->mtx);
129 } else
130 drv = ERR_PTR(-ENODEV);
131 dev_put(dev);
132 out:
133 mutex_unlock(&cfg80211_drv_mutex);
134 return drv;
135}
136
137void cfg80211_put_dev(struct cfg80211_registered_device *drv)
138{
139 BUG_ON(IS_ERR(drv));
140 mutex_unlock(&drv->mtx);
141}
142
143int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
144 char *newname)
145{
146 int idx, taken = -1, result, digits;
147
148 /* prohibit calling the thing phy%d when %d is not its number */
149 sscanf(newname, PHY_NAME "%d%n", &idx, &taken);
150 if (taken == strlen(newname) && idx != rdev->idx) {
151 /* count number of places needed to print idx */
152 digits = 1;
153 while (idx /= 10)
154 digits++;
155 /*
156 * deny the name if it is phy<idx> where <idx> is printed
157 * without leading zeroes. taken == strlen(newname) here
158 */
159 if (taken == strlen(PHY_NAME) + digits)
160 return -EINVAL;
161 }
162
163 /* this will check for collisions */
164 result = device_rename(&rdev->wiphy.dev, newname);
165 if (result)
166 return result;
167
168 if (!debugfs_rename(rdev->wiphy.debugfsdir->d_parent,
169 rdev->wiphy.debugfsdir,
170 rdev->wiphy.debugfsdir->d_parent,
171 newname))
172 printk(KERN_ERR "cfg80211: failed to rename debugfs dir to %s!\n",
173 newname);
174
175 nl80211_notify_dev_rename(rdev);
176
177 return 0;
178}
179
704232c2
JB
180/* exported functions */
181
182struct wiphy *wiphy_new(struct cfg80211_ops *ops, int sizeof_priv)
183{
184 struct cfg80211_registered_device *drv;
185 int alloc_size;
186
41ade00f
JB
187 WARN_ON(!ops->add_key && ops->del_key);
188 WARN_ON(ops->add_key && !ops->del_key);
189
704232c2
JB
190 alloc_size = sizeof(*drv) + sizeof_priv;
191
192 drv = kzalloc(alloc_size, GFP_KERNEL);
193 if (!drv)
194 return NULL;
195
196 drv->ops = ops;
197
198 mutex_lock(&cfg80211_drv_mutex);
199
a4d73ee1
JB
200 drv->idx = wiphy_counter;
201
202 /* now increase counter for the next device unless
203 * it has wrapped previously */
204 if (wiphy_counter >= 0)
205 wiphy_counter++;
206
207 mutex_unlock(&cfg80211_drv_mutex);
208
209 if (unlikely(drv->idx < 0)) {
704232c2
JB
210 /* ugh, wrapped! */
211 kfree(drv);
212 return NULL;
213 }
704232c2
JB
214
215 /* give it a proper name */
216 snprintf(drv->wiphy.dev.bus_id, BUS_ID_SIZE,
217 PHY_NAME "%d", drv->idx);
218
704232c2
JB
219 mutex_init(&drv->mtx);
220 mutex_init(&drv->devlist_mtx);
221 INIT_LIST_HEAD(&drv->netdev_list);
222
223 device_initialize(&drv->wiphy.dev);
224 drv->wiphy.dev.class = &ieee80211_class;
225 drv->wiphy.dev.platform_data = drv;
226
227 return &drv->wiphy;
228}
229EXPORT_SYMBOL(wiphy_new);
230
231int wiphy_register(struct wiphy *wiphy)
232{
233 struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
234 int res;
8318d78a
JB
235 enum ieee80211_band band;
236 struct ieee80211_supported_band *sband;
237 bool have_band = false;
238 int i;
239
240 /* sanity check supported bands/channels */
241 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
242 sband = wiphy->bands[band];
243 if (!sband)
244 continue;
245
246 sband->band = band;
247
248 if (!sband->n_channels || !sband->n_bitrates) {
249 WARN_ON(1);
250 return -EINVAL;
251 }
252
253 for (i = 0; i < sband->n_channels; i++) {
254 sband->channels[i].orig_flags =
255 sband->channels[i].flags;
256 sband->channels[i].orig_mag =
257 sband->channels[i].max_antenna_gain;
258 sband->channels[i].orig_mpwr =
259 sband->channels[i].max_power;
260 sband->channels[i].band = band;
261 }
262
263 have_band = true;
264 }
265
266 if (!have_band) {
267 WARN_ON(1);
268 return -EINVAL;
269 }
270
271 /* check and set up bitrates */
272 ieee80211_set_bitrate_flags(wiphy);
273
274 /* set up regulatory info */
275 wiphy_update_regulatory(wiphy);
704232c2
JB
276
277 mutex_lock(&cfg80211_drv_mutex);
278
704232c2
JB
279 res = device_add(&drv->wiphy.dev);
280 if (res)
281 goto out_unlock;
282
283 list_add(&drv->list, &cfg80211_drv_list);
284
285 /* add to debugfs */
286 drv->wiphy.debugfsdir =
287 debugfs_create_dir(wiphy_name(&drv->wiphy),
288 ieee80211_debugfs_dir);
289
290 res = 0;
291out_unlock:
292 mutex_unlock(&cfg80211_drv_mutex);
293 return res;
294}
295EXPORT_SYMBOL(wiphy_register);
296
297void wiphy_unregister(struct wiphy *wiphy)
298{
299 struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
300
f16bfc1c 301 /* protect the device list */
704232c2
JB
302 mutex_lock(&cfg80211_drv_mutex);
303
f16bfc1c
JB
304 BUG_ON(!list_empty(&drv->netdev_list));
305
306 /*
307 * Try to grab drv->mtx. If a command is still in progress,
308 * hopefully the driver will refuse it since it's tearing
309 * down the device already. We wait for this command to complete
310 * before unlinking the item from the list.
311 * Note: as codified by the BUG_ON above we cannot get here if
312 * a virtual interface is still associated. Hence, we can only
313 * get to lock contention here if userspace issues a command
314 * that identified the hardware by wiphy index.
315 */
704232c2 316 mutex_lock(&drv->mtx);
f16bfc1c 317 /* unlock again before freeing */
704232c2
JB
318 mutex_unlock(&drv->mtx);
319
f16bfc1c 320 list_del(&drv->list);
704232c2
JB
321 device_del(&drv->wiphy.dev);
322 debugfs_remove(drv->wiphy.debugfsdir);
323
324 mutex_unlock(&cfg80211_drv_mutex);
325}
326EXPORT_SYMBOL(wiphy_unregister);
327
328void cfg80211_dev_free(struct cfg80211_registered_device *drv)
329{
330 mutex_destroy(&drv->mtx);
331 mutex_destroy(&drv->devlist_mtx);
332 kfree(drv);
333}
334
335void wiphy_free(struct wiphy *wiphy)
336{
337 put_device(&wiphy->dev);
338}
339EXPORT_SYMBOL(wiphy_free);
340
341static int cfg80211_netdev_notifier_call(struct notifier_block * nb,
342 unsigned long state,
343 void *ndev)
344{
345 struct net_device *dev = ndev;
346 struct cfg80211_registered_device *rdev;
347
348 if (!dev->ieee80211_ptr)
349 return 0;
350
351 rdev = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
352
353 switch (state) {
354 case NETDEV_REGISTER:
355 mutex_lock(&rdev->devlist_mtx);
356 list_add(&dev->ieee80211_ptr->list, &rdev->netdev_list);
357 if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj,
358 "phy80211")) {
359 printk(KERN_ERR "wireless: failed to add phy80211 "
360 "symlink to netdev!\n");
361 }
362 dev->ieee80211_ptr->netdev = dev;
363 mutex_unlock(&rdev->devlist_mtx);
364 break;
365 case NETDEV_UNREGISTER:
366 mutex_lock(&rdev->devlist_mtx);
367 if (!list_empty(&dev->ieee80211_ptr->list)) {
368 sysfs_remove_link(&dev->dev.kobj, "phy80211");
369 list_del_init(&dev->ieee80211_ptr->list);
370 }
371 mutex_unlock(&rdev->devlist_mtx);
372 break;
373 }
374
375 return 0;
376}
377
378static struct notifier_block cfg80211_netdev_notifier = {
379 .notifier_call = cfg80211_netdev_notifier_call,
380};
381
382static int cfg80211_init(void)
383{
384 int err = wiphy_sysfs_init();
385 if (err)
386 goto out_fail_sysfs;
387
388 err = register_netdevice_notifier(&cfg80211_netdev_notifier);
389 if (err)
390 goto out_fail_notifier;
391
55682965
JB
392 err = nl80211_init();
393 if (err)
394 goto out_fail_nl80211;
395
704232c2
JB
396 ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
397
398 return 0;
399
55682965
JB
400out_fail_nl80211:
401 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
704232c2
JB
402out_fail_notifier:
403 wiphy_sysfs_exit();
404out_fail_sysfs:
405 return err;
406}
3a462465 407subsys_initcall(cfg80211_init);
704232c2
JB
408
409static void cfg80211_exit(void)
410{
411 debugfs_remove(ieee80211_debugfs_dir);
55682965 412 nl80211_exit();
704232c2
JB
413 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
414 wiphy_sysfs_exit();
415}
416module_exit(cfg80211_exit);