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