core.c 15 KB

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