xref: /linux/net/wireless/core.c (revision 5bdef865eb358b6f3760e25e591ae115e9eeddef)
1 /*
2  * This is the linux wireless configuration interface.
3  *
4  * Copyright 2006-2009		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/list.h>
11 #include <linux/nl80211.h>
12 #include <linux/debugfs.h>
13 #include <linux/notifier.h>
14 #include <linux/device.h>
15 #include <linux/rtnetlink.h>
16 #include <net/genetlink.h>
17 #include <net/cfg80211.h>
18 #include "nl80211.h"
19 #include "core.h"
20 #include "sysfs.h"
21 #include "debugfs.h"
22 
23 /* name for sysfs, %d is appended */
24 #define PHY_NAME "phy"
25 
26 MODULE_AUTHOR("Johannes Berg");
27 MODULE_LICENSE("GPL");
28 MODULE_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 */
33 LIST_HEAD(cfg80211_drv_list);
34 
35 /*
36  * This is used to protect the cfg80211_drv_list, cfg80211_regdomain,
37  * country_ie_regdomain, the reg_beacon_list and the the last regulatory
38  * request receipt (last_request).
39  */
40 DEFINE_MUTEX(cfg80211_mutex);
41 
42 /* for debugfs */
43 static struct dentry *ieee80211_debugfs_dir;
44 
45 /* requires cfg80211_mutex to be held! */
46 struct cfg80211_registered_device *cfg80211_drv_by_wiphy_idx(int wiphy_idx)
47 {
48 	struct cfg80211_registered_device *result = NULL, *drv;
49 
50 	if (!wiphy_idx_valid(wiphy_idx))
51 		return NULL;
52 
53 	assert_cfg80211_lock();
54 
55 	list_for_each_entry(drv, &cfg80211_drv_list, list) {
56 		if (drv->wiphy_idx == wiphy_idx) {
57 			result = drv;
58 			break;
59 		}
60 	}
61 
62 	return result;
63 }
64 
65 int get_wiphy_idx(struct wiphy *wiphy)
66 {
67 	struct cfg80211_registered_device *drv;
68 	if (!wiphy)
69 		return WIPHY_IDX_STALE;
70 	drv = wiphy_to_dev(wiphy);
71 	return drv->wiphy_idx;
72 }
73 
74 /* requires cfg80211_drv_mutex to be held! */
75 struct wiphy *wiphy_idx_to_wiphy(int wiphy_idx)
76 {
77 	struct cfg80211_registered_device *drv;
78 
79 	if (!wiphy_idx_valid(wiphy_idx))
80 		return NULL;
81 
82 	assert_cfg80211_lock();
83 
84 	drv = cfg80211_drv_by_wiphy_idx(wiphy_idx);
85 	if (!drv)
86 		return NULL;
87 	return &drv->wiphy;
88 }
89 
90 /* requires cfg80211_mutex to be held! */
91 struct cfg80211_registered_device *
92 __cfg80211_drv_from_info(struct genl_info *info)
93 {
94 	int ifindex;
95 	struct cfg80211_registered_device *bywiphyidx = NULL, *byifidx = NULL;
96 	struct net_device *dev;
97 	int err = -EINVAL;
98 
99 	assert_cfg80211_lock();
100 
101 	if (info->attrs[NL80211_ATTR_WIPHY]) {
102 		bywiphyidx = cfg80211_drv_by_wiphy_idx(
103 				nla_get_u32(info->attrs[NL80211_ATTR_WIPHY]));
104 		err = -ENODEV;
105 	}
106 
107 	if (info->attrs[NL80211_ATTR_IFINDEX]) {
108 		ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
109 		dev = dev_get_by_index(&init_net, ifindex);
110 		if (dev) {
111 			if (dev->ieee80211_ptr)
112 				byifidx =
113 					wiphy_to_dev(dev->ieee80211_ptr->wiphy);
114 			dev_put(dev);
115 		}
116 		err = -ENODEV;
117 	}
118 
119 	if (bywiphyidx && byifidx) {
120 		if (bywiphyidx != byifidx)
121 			return ERR_PTR(-EINVAL);
122 		else
123 			return bywiphyidx; /* == byifidx */
124 	}
125 	if (bywiphyidx)
126 		return bywiphyidx;
127 
128 	if (byifidx)
129 		return byifidx;
130 
131 	return ERR_PTR(err);
132 }
133 
134 struct cfg80211_registered_device *
135 cfg80211_get_dev_from_info(struct genl_info *info)
136 {
137 	struct cfg80211_registered_device *drv;
138 
139 	mutex_lock(&cfg80211_mutex);
140 	drv = __cfg80211_drv_from_info(info);
141 
142 	/* if it is not an error we grab the lock on
143 	 * it to assure it won't be going away while
144 	 * we operate on it */
145 	if (!IS_ERR(drv))
146 		mutex_lock(&drv->mtx);
147 
148 	mutex_unlock(&cfg80211_mutex);
149 
150 	return drv;
151 }
152 
153 struct cfg80211_registered_device *
154 cfg80211_get_dev_from_ifindex(int ifindex)
155 {
156 	struct cfg80211_registered_device *drv = ERR_PTR(-ENODEV);
157 	struct net_device *dev;
158 
159 	mutex_lock(&cfg80211_mutex);
160 	dev = dev_get_by_index(&init_net, ifindex);
161 	if (!dev)
162 		goto out;
163 	if (dev->ieee80211_ptr) {
164 		drv = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
165 		mutex_lock(&drv->mtx);
166 	} else
167 		drv = ERR_PTR(-ENODEV);
168 	dev_put(dev);
169  out:
170 	mutex_unlock(&cfg80211_mutex);
171 	return drv;
172 }
173 
174 void cfg80211_put_dev(struct cfg80211_registered_device *drv)
175 {
176 	BUG_ON(IS_ERR(drv));
177 	mutex_unlock(&drv->mtx);
178 }
179 
180 /* requires cfg80211_mutex to be held */
181 int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
182 			char *newname)
183 {
184 	struct cfg80211_registered_device *drv;
185 	int wiphy_idx, taken = -1, result, digits;
186 
187 	assert_cfg80211_lock();
188 
189 	/* prohibit calling the thing phy%d when %d is not its number */
190 	sscanf(newname, PHY_NAME "%d%n", &wiphy_idx, &taken);
191 	if (taken == strlen(newname) && wiphy_idx != rdev->wiphy_idx) {
192 		/* count number of places needed to print wiphy_idx */
193 		digits = 1;
194 		while (wiphy_idx /= 10)
195 			digits++;
196 		/*
197 		 * deny the name if it is phy<idx> where <idx> is printed
198 		 * without leading zeroes. taken == strlen(newname) here
199 		 */
200 		if (taken == strlen(PHY_NAME) + digits)
201 			return -EINVAL;
202 	}
203 
204 
205 	/* Ignore nop renames */
206 	if (strcmp(newname, dev_name(&rdev->wiphy.dev)) == 0)
207 		return 0;
208 
209 	/* Ensure another device does not already have this name. */
210 	list_for_each_entry(drv, &cfg80211_drv_list, list)
211 		if (strcmp(newname, dev_name(&drv->wiphy.dev)) == 0)
212 			return -EINVAL;
213 
214 	result = device_rename(&rdev->wiphy.dev, newname);
215 	if (result)
216 		return result;
217 
218 	if (rdev->wiphy.debugfsdir &&
219 	    !debugfs_rename(rdev->wiphy.debugfsdir->d_parent,
220 			    rdev->wiphy.debugfsdir,
221 			    rdev->wiphy.debugfsdir->d_parent,
222 			    newname))
223 		printk(KERN_ERR "cfg80211: failed to rename debugfs dir to %s!\n",
224 		       newname);
225 
226 	nl80211_notify_dev_rename(rdev);
227 
228 	return 0;
229 }
230 
231 static void cfg80211_rfkill_poll(struct rfkill *rfkill, void *data)
232 {
233 	struct cfg80211_registered_device *drv = data;
234 
235 	drv->ops->rfkill_poll(&drv->wiphy);
236 }
237 
238 static int cfg80211_rfkill_set_block(void *data, bool blocked)
239 {
240 	struct cfg80211_registered_device *drv = data;
241 	struct wireless_dev *wdev;
242 
243 	if (!blocked)
244 		return 0;
245 
246 	rtnl_lock();
247 	mutex_lock(&drv->devlist_mtx);
248 
249 	list_for_each_entry(wdev, &drv->netdev_list, list)
250 		dev_close(wdev->netdev);
251 
252 	mutex_unlock(&drv->devlist_mtx);
253 	rtnl_unlock();
254 
255 	return 0;
256 }
257 
258 static void cfg80211_rfkill_sync_work(struct work_struct *work)
259 {
260 	struct cfg80211_registered_device *drv;
261 
262 	drv = container_of(work, struct cfg80211_registered_device, rfkill_sync);
263 	cfg80211_rfkill_set_block(drv, rfkill_blocked(drv->rfkill));
264 }
265 
266 /* exported functions */
267 
268 struct wiphy *wiphy_new(const struct cfg80211_ops *ops, int sizeof_priv)
269 {
270 	static int wiphy_counter;
271 
272 	struct cfg80211_registered_device *drv;
273 	int alloc_size;
274 
275 	WARN_ON(!ops->add_key && ops->del_key);
276 	WARN_ON(ops->add_key && !ops->del_key);
277 
278 	alloc_size = sizeof(*drv) + sizeof_priv;
279 
280 	drv = kzalloc(alloc_size, GFP_KERNEL);
281 	if (!drv)
282 		return NULL;
283 
284 	drv->ops = ops;
285 
286 	mutex_lock(&cfg80211_mutex);
287 
288 	drv->wiphy_idx = wiphy_counter++;
289 
290 	if (unlikely(!wiphy_idx_valid(drv->wiphy_idx))) {
291 		wiphy_counter--;
292 		mutex_unlock(&cfg80211_mutex);
293 		/* ugh, wrapped! */
294 		kfree(drv);
295 		return NULL;
296 	}
297 
298 	mutex_unlock(&cfg80211_mutex);
299 
300 	/* give it a proper name */
301 	dev_set_name(&drv->wiphy.dev, PHY_NAME "%d", drv->wiphy_idx);
302 
303 	mutex_init(&drv->mtx);
304 	mutex_init(&drv->devlist_mtx);
305 	INIT_LIST_HEAD(&drv->netdev_list);
306 	spin_lock_init(&drv->bss_lock);
307 	INIT_LIST_HEAD(&drv->bss_list);
308 
309 	device_initialize(&drv->wiphy.dev);
310 	drv->wiphy.dev.class = &ieee80211_class;
311 	drv->wiphy.dev.platform_data = drv;
312 
313 	drv->rfkill_ops.set_block = cfg80211_rfkill_set_block;
314 	drv->rfkill = rfkill_alloc(dev_name(&drv->wiphy.dev),
315 				   &drv->wiphy.dev, RFKILL_TYPE_WLAN,
316 				   &drv->rfkill_ops, drv);
317 
318 	if (!drv->rfkill) {
319 		kfree(drv);
320 		return NULL;
321 	}
322 
323 	INIT_WORK(&drv->rfkill_sync, cfg80211_rfkill_sync_work);
324 
325 	/*
326 	 * Initialize wiphy parameters to IEEE 802.11 MIB default values.
327 	 * Fragmentation and RTS threshold are disabled by default with the
328 	 * special -1 value.
329 	 */
330 	drv->wiphy.retry_short = 7;
331 	drv->wiphy.retry_long = 4;
332 	drv->wiphy.frag_threshold = (u32) -1;
333 	drv->wiphy.rts_threshold = (u32) -1;
334 
335 	return &drv->wiphy;
336 }
337 EXPORT_SYMBOL(wiphy_new);
338 
339 int wiphy_register(struct wiphy *wiphy)
340 {
341 	struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
342 	int res;
343 	enum ieee80211_band band;
344 	struct ieee80211_supported_band *sband;
345 	bool have_band = false;
346 	int i;
347 	u16 ifmodes = wiphy->interface_modes;
348 
349 	if (WARN_ON(wiphy->max_scan_ssids < 1))
350 		return -EINVAL;
351 
352 	/* sanity check ifmodes */
353 	WARN_ON(!ifmodes);
354 	ifmodes &= ((1 << __NL80211_IFTYPE_AFTER_LAST) - 1) & ~1;
355 	if (WARN_ON(ifmodes != wiphy->interface_modes))
356 		wiphy->interface_modes = ifmodes;
357 
358 	/* sanity check supported bands/channels */
359 	for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
360 		sband = wiphy->bands[band];
361 		if (!sband)
362 			continue;
363 
364 		sband->band = band;
365 
366 		if (WARN_ON(!sband->n_channels || !sband->n_bitrates))
367 			return -EINVAL;
368 
369 		/*
370 		 * Since we use a u32 for rate bitmaps in
371 		 * ieee80211_get_response_rate, we cannot
372 		 * have more than 32 legacy rates.
373 		 */
374 		if (WARN_ON(sband->n_bitrates > 32))
375 			return -EINVAL;
376 
377 		for (i = 0; i < sband->n_channels; i++) {
378 			sband->channels[i].orig_flags =
379 				sband->channels[i].flags;
380 			sband->channels[i].orig_mag =
381 				sband->channels[i].max_antenna_gain;
382 			sband->channels[i].orig_mpwr =
383 				sband->channels[i].max_power;
384 			sband->channels[i].band = band;
385 		}
386 
387 		have_band = true;
388 	}
389 
390 	if (!have_band) {
391 		WARN_ON(1);
392 		return -EINVAL;
393 	}
394 
395 	/* check and set up bitrates */
396 	ieee80211_set_bitrate_flags(wiphy);
397 
398 	res = device_add(&drv->wiphy.dev);
399 	if (res)
400 		return res;
401 
402 	res = rfkill_register(drv->rfkill);
403 	if (res)
404 		goto out_rm_dev;
405 
406 	mutex_lock(&cfg80211_mutex);
407 
408 	/* set up regulatory info */
409 	wiphy_update_regulatory(wiphy, NL80211_REGDOM_SET_BY_CORE);
410 
411 	list_add(&drv->list, &cfg80211_drv_list);
412 
413 	mutex_unlock(&cfg80211_mutex);
414 
415 	/* add to debugfs */
416 	drv->wiphy.debugfsdir =
417 		debugfs_create_dir(wiphy_name(&drv->wiphy),
418 				   ieee80211_debugfs_dir);
419 	if (IS_ERR(drv->wiphy.debugfsdir))
420 		drv->wiphy.debugfsdir = NULL;
421 
422 	if (wiphy->custom_regulatory) {
423 		struct regulatory_request request;
424 
425 		request.wiphy_idx = get_wiphy_idx(wiphy);
426 		request.initiator = NL80211_REGDOM_SET_BY_DRIVER;
427 		request.alpha2[0] = '9';
428 		request.alpha2[1] = '9';
429 
430 		nl80211_send_reg_change_event(&request);
431 	}
432 
433 	cfg80211_debugfs_drv_add(drv);
434 
435 	return 0;
436 
437  out_rm_dev:
438 	device_del(&drv->wiphy.dev);
439 	return res;
440 }
441 EXPORT_SYMBOL(wiphy_register);
442 
443 void wiphy_rfkill_start_polling(struct wiphy *wiphy)
444 {
445 	struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
446 
447 	if (!drv->ops->rfkill_poll)
448 		return;
449 	drv->rfkill_ops.poll = cfg80211_rfkill_poll;
450 	rfkill_resume_polling(drv->rfkill);
451 }
452 EXPORT_SYMBOL(wiphy_rfkill_start_polling);
453 
454 void wiphy_rfkill_stop_polling(struct wiphy *wiphy)
455 {
456 	struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
457 
458 	rfkill_pause_polling(drv->rfkill);
459 }
460 EXPORT_SYMBOL(wiphy_rfkill_stop_polling);
461 
462 void wiphy_unregister(struct wiphy *wiphy)
463 {
464 	struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
465 
466 	rfkill_unregister(drv->rfkill);
467 
468 	/* protect the device list */
469 	mutex_lock(&cfg80211_mutex);
470 
471 	BUG_ON(!list_empty(&drv->netdev_list));
472 
473 	/*
474 	 * Try to grab drv->mtx. If a command is still in progress,
475 	 * hopefully the driver will refuse it since it's tearing
476 	 * down the device already. We wait for this command to complete
477 	 * before unlinking the item from the list.
478 	 * Note: as codified by the BUG_ON above we cannot get here if
479 	 * a virtual interface is still associated. Hence, we can only
480 	 * get to lock contention here if userspace issues a command
481 	 * that identified the hardware by wiphy index.
482 	 */
483 	mutex_lock(&drv->mtx);
484 	/* unlock again before freeing */
485 	mutex_unlock(&drv->mtx);
486 
487 	cfg80211_debugfs_drv_del(drv);
488 
489 	/* If this device got a regulatory hint tell core its
490 	 * free to listen now to a new shiny device regulatory hint */
491 	reg_device_remove(wiphy);
492 
493 	list_del(&drv->list);
494 	device_del(&drv->wiphy.dev);
495 	debugfs_remove(drv->wiphy.debugfsdir);
496 
497 	mutex_unlock(&cfg80211_mutex);
498 }
499 EXPORT_SYMBOL(wiphy_unregister);
500 
501 void cfg80211_dev_free(struct cfg80211_registered_device *drv)
502 {
503 	struct cfg80211_internal_bss *scan, *tmp;
504 	rfkill_destroy(drv->rfkill);
505 	mutex_destroy(&drv->mtx);
506 	mutex_destroy(&drv->devlist_mtx);
507 	list_for_each_entry_safe(scan, tmp, &drv->bss_list, list)
508 		cfg80211_put_bss(&scan->pub);
509 	kfree(drv);
510 }
511 
512 void wiphy_free(struct wiphy *wiphy)
513 {
514 	put_device(&wiphy->dev);
515 }
516 EXPORT_SYMBOL(wiphy_free);
517 
518 void wiphy_rfkill_set_hw_state(struct wiphy *wiphy, bool blocked)
519 {
520 	struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
521 
522 	if (rfkill_set_hw_state(drv->rfkill, blocked))
523 		schedule_work(&drv->rfkill_sync);
524 }
525 EXPORT_SYMBOL(wiphy_rfkill_set_hw_state);
526 
527 static int cfg80211_netdev_notifier_call(struct notifier_block * nb,
528 					 unsigned long state,
529 					 void *ndev)
530 {
531 	struct net_device *dev = ndev;
532 	struct cfg80211_registered_device *rdev;
533 
534 	if (!dev->ieee80211_ptr)
535 		return NOTIFY_DONE;
536 
537 	rdev = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
538 
539 	WARN_ON(dev->ieee80211_ptr->iftype == NL80211_IFTYPE_UNSPECIFIED);
540 
541 	switch (state) {
542 	case NETDEV_REGISTER:
543 		mutex_lock(&rdev->devlist_mtx);
544 		list_add(&dev->ieee80211_ptr->list, &rdev->netdev_list);
545 		if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj,
546 				      "phy80211")) {
547 			printk(KERN_ERR "wireless: failed to add phy80211 "
548 				"symlink to netdev!\n");
549 		}
550 		dev->ieee80211_ptr->netdev = dev;
551 #ifdef CONFIG_WIRELESS_EXT
552 		dev->ieee80211_ptr->wext.default_key = -1;
553 		dev->ieee80211_ptr->wext.default_mgmt_key = -1;
554 #endif
555 		mutex_unlock(&rdev->devlist_mtx);
556 		break;
557 	case NETDEV_GOING_DOWN:
558 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
559 			break;
560 		if (!dev->ieee80211_ptr->ssid_len)
561 			break;
562 		cfg80211_leave_ibss(rdev, dev, true);
563 		break;
564 	case NETDEV_UP:
565 #ifdef CONFIG_WIRELESS_EXT
566 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
567 			break;
568 		if (!dev->ieee80211_ptr->wext.ibss.ssid_len)
569 			break;
570 		cfg80211_join_ibss(rdev, dev, &dev->ieee80211_ptr->wext.ibss);
571 		break;
572 #endif
573 	case NETDEV_UNREGISTER:
574 		mutex_lock(&rdev->devlist_mtx);
575 		if (!list_empty(&dev->ieee80211_ptr->list)) {
576 			sysfs_remove_link(&dev->dev.kobj, "phy80211");
577 			list_del_init(&dev->ieee80211_ptr->list);
578 		}
579 		mutex_unlock(&rdev->devlist_mtx);
580 		break;
581 	case NETDEV_PRE_UP:
582 		if (rfkill_blocked(rdev->rfkill))
583 			return notifier_from_errno(-ERFKILL);
584 		break;
585 	}
586 
587 	return NOTIFY_DONE;
588 }
589 
590 static struct notifier_block cfg80211_netdev_notifier = {
591 	.notifier_call = cfg80211_netdev_notifier_call,
592 };
593 
594 static int cfg80211_init(void)
595 {
596 	int err;
597 
598 	err = wiphy_sysfs_init();
599 	if (err)
600 		goto out_fail_sysfs;
601 
602 	err = register_netdevice_notifier(&cfg80211_netdev_notifier);
603 	if (err)
604 		goto out_fail_notifier;
605 
606 	err = nl80211_init();
607 	if (err)
608 		goto out_fail_nl80211;
609 
610 	ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
611 
612 	err = regulatory_init();
613 	if (err)
614 		goto out_fail_reg;
615 
616 	return 0;
617 
618 out_fail_reg:
619 	debugfs_remove(ieee80211_debugfs_dir);
620 out_fail_nl80211:
621 	unregister_netdevice_notifier(&cfg80211_netdev_notifier);
622 out_fail_notifier:
623 	wiphy_sysfs_exit();
624 out_fail_sysfs:
625 	return err;
626 }
627 
628 subsys_initcall(cfg80211_init);
629 
630 static void cfg80211_exit(void)
631 {
632 	debugfs_remove(ieee80211_debugfs_dir);
633 	nl80211_exit();
634 	unregister_netdevice_notifier(&cfg80211_netdev_notifier);
635 	wiphy_sysfs_exit();
636 	regulatory_exit();
637 }
638 module_exit(cfg80211_exit);
639