core.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. /*
  2. * This is the linux wireless configuration interface.
  3. *
  4. * Copyright 2006-2010 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/slab.h>
  11. #include <linux/nl80211.h>
  12. #include <linux/debugfs.h>
  13. #include <linux/notifier.h>
  14. #include <linux/device.h>
  15. #include <linux/etherdevice.h>
  16. #include <linux/rtnetlink.h>
  17. #include <linux/sched.h>
  18. #include <net/genetlink.h>
  19. #include <net/cfg80211.h>
  20. #include "nl80211.h"
  21. #include "core.h"
  22. #include "sysfs.h"
  23. #include "debugfs.h"
  24. #include "wext-compat.h"
  25. #include "ethtool.h"
  26. /* name for sysfs, %d is appended */
  27. #define PHY_NAME "phy"
  28. MODULE_AUTHOR("Johannes Berg");
  29. MODULE_LICENSE("GPL");
  30. MODULE_DESCRIPTION("wireless configuration support");
  31. /* RCU-protected (and cfg80211_mutex for writers) */
  32. LIST_HEAD(cfg80211_rdev_list);
  33. int cfg80211_rdev_list_generation;
  34. DEFINE_MUTEX(cfg80211_mutex);
  35. /* for debugfs */
  36. static struct dentry *ieee80211_debugfs_dir;
  37. /* for the cleanup, scan and event works */
  38. struct workqueue_struct *cfg80211_wq;
  39. /* requires cfg80211_mutex to be held! */
  40. struct cfg80211_registered_device *cfg80211_rdev_by_wiphy_idx(int wiphy_idx)
  41. {
  42. struct cfg80211_registered_device *result = NULL, *rdev;
  43. if (!wiphy_idx_valid(wiphy_idx))
  44. return NULL;
  45. assert_cfg80211_lock();
  46. list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
  47. if (rdev->wiphy_idx == wiphy_idx) {
  48. result = rdev;
  49. break;
  50. }
  51. }
  52. return result;
  53. }
  54. int get_wiphy_idx(struct wiphy *wiphy)
  55. {
  56. struct cfg80211_registered_device *rdev;
  57. if (!wiphy)
  58. return WIPHY_IDX_STALE;
  59. rdev = wiphy_to_dev(wiphy);
  60. return rdev->wiphy_idx;
  61. }
  62. /* requires cfg80211_rdev_mutex to be held! */
  63. struct wiphy *wiphy_idx_to_wiphy(int wiphy_idx)
  64. {
  65. struct cfg80211_registered_device *rdev;
  66. if (!wiphy_idx_valid(wiphy_idx))
  67. return NULL;
  68. assert_cfg80211_lock();
  69. rdev = cfg80211_rdev_by_wiphy_idx(wiphy_idx);
  70. if (!rdev)
  71. return NULL;
  72. return &rdev->wiphy;
  73. }
  74. /* requires cfg80211_mutex to be held! */
  75. struct cfg80211_registered_device *
  76. __cfg80211_rdev_from_info(struct genl_info *info)
  77. {
  78. int ifindex;
  79. struct cfg80211_registered_device *bywiphyidx = NULL, *byifidx = NULL;
  80. struct net_device *dev;
  81. int err = -EINVAL;
  82. assert_cfg80211_lock();
  83. if (info->attrs[NL80211_ATTR_WIPHY]) {
  84. bywiphyidx = cfg80211_rdev_by_wiphy_idx(
  85. nla_get_u32(info->attrs[NL80211_ATTR_WIPHY]));
  86. err = -ENODEV;
  87. }
  88. if (info->attrs[NL80211_ATTR_IFINDEX]) {
  89. ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
  90. dev = dev_get_by_index(genl_info_net(info), ifindex);
  91. if (dev) {
  92. if (dev->ieee80211_ptr)
  93. byifidx =
  94. wiphy_to_dev(dev->ieee80211_ptr->wiphy);
  95. dev_put(dev);
  96. }
  97. err = -ENODEV;
  98. }
  99. if (bywiphyidx && byifidx) {
  100. if (bywiphyidx != byifidx)
  101. return ERR_PTR(-EINVAL);
  102. else
  103. return bywiphyidx; /* == byifidx */
  104. }
  105. if (bywiphyidx)
  106. return bywiphyidx;
  107. if (byifidx)
  108. return byifidx;
  109. return ERR_PTR(err);
  110. }
  111. struct cfg80211_registered_device *
  112. cfg80211_get_dev_from_info(struct genl_info *info)
  113. {
  114. struct cfg80211_registered_device *rdev;
  115. mutex_lock(&cfg80211_mutex);
  116. rdev = __cfg80211_rdev_from_info(info);
  117. /* if it is not an error we grab the lock on
  118. * it to assure it won't be going away while
  119. * we operate on it */
  120. if (!IS_ERR(rdev))
  121. mutex_lock(&rdev->mtx);
  122. mutex_unlock(&cfg80211_mutex);
  123. return rdev;
  124. }
  125. struct cfg80211_registered_device *
  126. cfg80211_get_dev_from_ifindex(struct net *net, int ifindex)
  127. {
  128. struct cfg80211_registered_device *rdev = ERR_PTR(-ENODEV);
  129. struct net_device *dev;
  130. mutex_lock(&cfg80211_mutex);
  131. dev = dev_get_by_index(net, ifindex);
  132. if (!dev)
  133. goto out;
  134. if (dev->ieee80211_ptr) {
  135. rdev = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
  136. mutex_lock(&rdev->mtx);
  137. } else
  138. rdev = ERR_PTR(-ENODEV);
  139. dev_put(dev);
  140. out:
  141. mutex_unlock(&cfg80211_mutex);
  142. return rdev;
  143. }
  144. /* requires cfg80211_mutex to be held */
  145. int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
  146. char *newname)
  147. {
  148. struct cfg80211_registered_device *rdev2;
  149. int result;
  150. assert_cfg80211_lock();
  151. /* Ignore nop renames */
  152. if (strcmp(newname, dev_name(&rdev->wiphy.dev)) == 0)
  153. return 0;
  154. /* Ensure another device does not already have this name. */
  155. list_for_each_entry(rdev2, &cfg80211_rdev_list, list)
  156. if (strcmp(newname, dev_name(&rdev2->wiphy.dev)) == 0)
  157. return -EEXIST;
  158. result = device_rename(&rdev->wiphy.dev, newname);
  159. if (result)
  160. return result;
  161. if (rdev->wiphy.debugfsdir &&
  162. !debugfs_rename(rdev->wiphy.debugfsdir->d_parent,
  163. rdev->wiphy.debugfsdir,
  164. rdev->wiphy.debugfsdir->d_parent,
  165. newname))
  166. printk(KERN_ERR "cfg80211: failed to rename debugfs dir to %s!\n",
  167. newname);
  168. nl80211_notify_dev_rename(rdev);
  169. return 0;
  170. }
  171. int cfg80211_switch_netns(struct cfg80211_registered_device *rdev,
  172. struct net *net)
  173. {
  174. struct wireless_dev *wdev;
  175. int err = 0;
  176. if (!(rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK))
  177. return -EOPNOTSUPP;
  178. list_for_each_entry(wdev, &rdev->netdev_list, list) {
  179. wdev->netdev->features &= ~NETIF_F_NETNS_LOCAL;
  180. err = dev_change_net_namespace(wdev->netdev, net, "wlan%d");
  181. if (err)
  182. break;
  183. wdev->netdev->features |= NETIF_F_NETNS_LOCAL;
  184. }
  185. if (err) {
  186. /* failed -- clean up to old netns */
  187. net = wiphy_net(&rdev->wiphy);
  188. list_for_each_entry_continue_reverse(wdev, &rdev->netdev_list,
  189. list) {
  190. wdev->netdev->features &= ~NETIF_F_NETNS_LOCAL;
  191. err = dev_change_net_namespace(wdev->netdev, net,
  192. "wlan%d");
  193. WARN_ON(err);
  194. wdev->netdev->features |= NETIF_F_NETNS_LOCAL;
  195. }
  196. return err;
  197. }
  198. wiphy_net_set(&rdev->wiphy, net);
  199. err = device_rename(&rdev->wiphy.dev, dev_name(&rdev->wiphy.dev));
  200. WARN_ON(err);
  201. return 0;
  202. }
  203. static void cfg80211_rfkill_poll(struct rfkill *rfkill, void *data)
  204. {
  205. struct cfg80211_registered_device *rdev = data;
  206. rdev->ops->rfkill_poll(&rdev->wiphy);
  207. }
  208. static int cfg80211_rfkill_set_block(void *data, bool blocked)
  209. {
  210. struct cfg80211_registered_device *rdev = data;
  211. struct wireless_dev *wdev;
  212. if (!blocked)
  213. return 0;
  214. rtnl_lock();
  215. mutex_lock(&rdev->devlist_mtx);
  216. list_for_each_entry(wdev, &rdev->netdev_list, list)
  217. dev_close(wdev->netdev);
  218. mutex_unlock(&rdev->devlist_mtx);
  219. rtnl_unlock();
  220. return 0;
  221. }
  222. static void cfg80211_rfkill_sync_work(struct work_struct *work)
  223. {
  224. struct cfg80211_registered_device *rdev;
  225. rdev = container_of(work, struct cfg80211_registered_device, rfkill_sync);
  226. cfg80211_rfkill_set_block(rdev, rfkill_blocked(rdev->rfkill));
  227. }
  228. static void cfg80211_event_work(struct work_struct *work)
  229. {
  230. struct cfg80211_registered_device *rdev;
  231. rdev = container_of(work, struct cfg80211_registered_device,
  232. event_work);
  233. rtnl_lock();
  234. cfg80211_lock_rdev(rdev);
  235. cfg80211_process_rdev_events(rdev);
  236. cfg80211_unlock_rdev(rdev);
  237. rtnl_unlock();
  238. }
  239. /* exported functions */
  240. struct wiphy *wiphy_new(const struct cfg80211_ops *ops, int sizeof_priv)
  241. {
  242. static int wiphy_counter;
  243. int i;
  244. struct cfg80211_registered_device *rdev, *rdev2;
  245. int alloc_size;
  246. char nname[IFNAMSIZ + 1];
  247. bool found = false;
  248. WARN_ON(ops->add_key && (!ops->del_key || !ops->set_default_key));
  249. WARN_ON(ops->auth && (!ops->assoc || !ops->deauth || !ops->disassoc));
  250. WARN_ON(ops->connect && !ops->disconnect);
  251. WARN_ON(ops->join_ibss && !ops->leave_ibss);
  252. WARN_ON(ops->add_virtual_intf && !ops->del_virtual_intf);
  253. WARN_ON(ops->add_station && !ops->del_station);
  254. WARN_ON(ops->add_mpath && !ops->del_mpath);
  255. alloc_size = sizeof(*rdev) + sizeof_priv;
  256. rdev = kzalloc(alloc_size, GFP_KERNEL);
  257. if (!rdev)
  258. return NULL;
  259. rdev->ops = ops;
  260. mutex_lock(&cfg80211_mutex);
  261. rdev->wiphy_idx = wiphy_counter++;
  262. if (unlikely(!wiphy_idx_valid(rdev->wiphy_idx))) {
  263. wiphy_counter--;
  264. goto too_many_devs;
  265. }
  266. /* 64k wiphy devices is enough for anyone! */
  267. for (i = 0; i < 0xFFFF; i++) {
  268. found = false;
  269. snprintf(nname, sizeof(nname)-1, PHY_NAME "%d", i);
  270. nname[sizeof(nname)-1] = 0;
  271. list_for_each_entry(rdev2, &cfg80211_rdev_list, list)
  272. if (strcmp(nname, dev_name(&rdev2->wiphy.dev)) == 0) {
  273. found = true;
  274. break;
  275. }
  276. if (!found)
  277. break;
  278. }
  279. if (unlikely(found)) {
  280. too_many_devs:
  281. mutex_unlock(&cfg80211_mutex);
  282. /* ugh, too many devices already! */
  283. kfree(rdev);
  284. return NULL;
  285. }
  286. /* give it a proper name */
  287. dev_set_name(&rdev->wiphy.dev, "%s", nname);
  288. mutex_unlock(&cfg80211_mutex);
  289. mutex_init(&rdev->mtx);
  290. mutex_init(&rdev->devlist_mtx);
  291. INIT_LIST_HEAD(&rdev->netdev_list);
  292. spin_lock_init(&rdev->bss_lock);
  293. INIT_LIST_HEAD(&rdev->bss_list);
  294. INIT_WORK(&rdev->scan_done_wk, __cfg80211_scan_done);
  295. #ifdef CONFIG_CFG80211_WEXT
  296. rdev->wiphy.wext = &cfg80211_wext_handler;
  297. #endif
  298. device_initialize(&rdev->wiphy.dev);
  299. rdev->wiphy.dev.class = &ieee80211_class;
  300. rdev->wiphy.dev.platform_data = rdev;
  301. #ifdef CONFIG_CFG80211_DEFAULT_PS
  302. rdev->wiphy.flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
  303. #endif
  304. wiphy_net_set(&rdev->wiphy, &init_net);
  305. rdev->rfkill_ops.set_block = cfg80211_rfkill_set_block;
  306. rdev->rfkill = rfkill_alloc(dev_name(&rdev->wiphy.dev),
  307. &rdev->wiphy.dev, RFKILL_TYPE_WLAN,
  308. &rdev->rfkill_ops, rdev);
  309. if (!rdev->rfkill) {
  310. kfree(rdev);
  311. return NULL;
  312. }
  313. INIT_WORK(&rdev->rfkill_sync, cfg80211_rfkill_sync_work);
  314. INIT_WORK(&rdev->conn_work, cfg80211_conn_work);
  315. INIT_WORK(&rdev->event_work, cfg80211_event_work);
  316. init_waitqueue_head(&rdev->dev_wait);
  317. /*
  318. * Initialize wiphy parameters to IEEE 802.11 MIB default values.
  319. * Fragmentation and RTS threshold are disabled by default with the
  320. * special -1 value.
  321. */
  322. rdev->wiphy.retry_short = 7;
  323. rdev->wiphy.retry_long = 4;
  324. rdev->wiphy.frag_threshold = (u32) -1;
  325. rdev->wiphy.rts_threshold = (u32) -1;
  326. rdev->wiphy.coverage_class = 0;
  327. return &rdev->wiphy;
  328. }
  329. EXPORT_SYMBOL(wiphy_new);
  330. int wiphy_register(struct wiphy *wiphy)
  331. {
  332. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  333. int res;
  334. enum ieee80211_band band;
  335. struct ieee80211_supported_band *sband;
  336. bool have_band = false;
  337. int i;
  338. u16 ifmodes = wiphy->interface_modes;
  339. if (WARN_ON(wiphy->addresses && !wiphy->n_addresses))
  340. return -EINVAL;
  341. if (WARN_ON(wiphy->addresses &&
  342. !is_zero_ether_addr(wiphy->perm_addr) &&
  343. memcmp(wiphy->perm_addr, wiphy->addresses[0].addr,
  344. ETH_ALEN)))
  345. return -EINVAL;
  346. if (wiphy->addresses)
  347. memcpy(wiphy->perm_addr, wiphy->addresses[0].addr, ETH_ALEN);
  348. /* sanity check ifmodes */
  349. WARN_ON(!ifmodes);
  350. ifmodes &= ((1 << NUM_NL80211_IFTYPES) - 1) & ~1;
  351. if (WARN_ON(ifmodes != wiphy->interface_modes))
  352. wiphy->interface_modes = ifmodes;
  353. /* sanity check supported bands/channels */
  354. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  355. sband = wiphy->bands[band];
  356. if (!sband)
  357. continue;
  358. sband->band = band;
  359. if (WARN_ON(!sband->n_channels || !sband->n_bitrates))
  360. return -EINVAL;
  361. /*
  362. * Since we use a u32 for rate bitmaps in
  363. * ieee80211_get_response_rate, we cannot
  364. * have more than 32 legacy rates.
  365. */
  366. if (WARN_ON(sband->n_bitrates > 32))
  367. return -EINVAL;
  368. for (i = 0; i < sband->n_channels; i++) {
  369. sband->channels[i].orig_flags =
  370. sband->channels[i].flags;
  371. sband->channels[i].orig_mag =
  372. sband->channels[i].max_antenna_gain;
  373. sband->channels[i].orig_mpwr =
  374. sband->channels[i].max_power;
  375. sband->channels[i].band = band;
  376. }
  377. have_band = true;
  378. }
  379. if (!have_band) {
  380. WARN_ON(1);
  381. return -EINVAL;
  382. }
  383. /* check and set up bitrates */
  384. ieee80211_set_bitrate_flags(wiphy);
  385. mutex_lock(&cfg80211_mutex);
  386. res = device_add(&rdev->wiphy.dev);
  387. if (res) {
  388. mutex_unlock(&cfg80211_mutex);
  389. return res;
  390. }
  391. /* set up regulatory info */
  392. wiphy_update_regulatory(wiphy, NL80211_REGDOM_SET_BY_CORE);
  393. list_add_rcu(&rdev->list, &cfg80211_rdev_list);
  394. cfg80211_rdev_list_generation++;
  395. /* add to debugfs */
  396. rdev->wiphy.debugfsdir =
  397. debugfs_create_dir(wiphy_name(&rdev->wiphy),
  398. ieee80211_debugfs_dir);
  399. if (IS_ERR(rdev->wiphy.debugfsdir))
  400. rdev->wiphy.debugfsdir = NULL;
  401. if (wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY) {
  402. struct regulatory_request request;
  403. request.wiphy_idx = get_wiphy_idx(wiphy);
  404. request.initiator = NL80211_REGDOM_SET_BY_DRIVER;
  405. request.alpha2[0] = '9';
  406. request.alpha2[1] = '9';
  407. nl80211_send_reg_change_event(&request);
  408. }
  409. cfg80211_debugfs_rdev_add(rdev);
  410. mutex_unlock(&cfg80211_mutex);
  411. /*
  412. * due to a locking dependency this has to be outside of the
  413. * cfg80211_mutex lock
  414. */
  415. res = rfkill_register(rdev->rfkill);
  416. if (res)
  417. goto out_rm_dev;
  418. return 0;
  419. out_rm_dev:
  420. device_del(&rdev->wiphy.dev);
  421. return res;
  422. }
  423. EXPORT_SYMBOL(wiphy_register);
  424. void wiphy_rfkill_start_polling(struct wiphy *wiphy)
  425. {
  426. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  427. if (!rdev->ops->rfkill_poll)
  428. return;
  429. rdev->rfkill_ops.poll = cfg80211_rfkill_poll;
  430. rfkill_resume_polling(rdev->rfkill);
  431. }
  432. EXPORT_SYMBOL(wiphy_rfkill_start_polling);
  433. void wiphy_rfkill_stop_polling(struct wiphy *wiphy)
  434. {
  435. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  436. rfkill_pause_polling(rdev->rfkill);
  437. }
  438. EXPORT_SYMBOL(wiphy_rfkill_stop_polling);
  439. void wiphy_unregister(struct wiphy *wiphy)
  440. {
  441. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  442. rfkill_unregister(rdev->rfkill);
  443. /* protect the device list */
  444. mutex_lock(&cfg80211_mutex);
  445. wait_event(rdev->dev_wait, ({
  446. int __count;
  447. mutex_lock(&rdev->devlist_mtx);
  448. __count = rdev->opencount;
  449. mutex_unlock(&rdev->devlist_mtx);
  450. __count == 0;}));
  451. mutex_lock(&rdev->devlist_mtx);
  452. BUG_ON(!list_empty(&rdev->netdev_list));
  453. mutex_unlock(&rdev->devlist_mtx);
  454. /*
  455. * First remove the hardware from everywhere, this makes
  456. * it impossible to find from userspace.
  457. */
  458. debugfs_remove_recursive(rdev->wiphy.debugfsdir);
  459. list_del_rcu(&rdev->list);
  460. synchronize_rcu();
  461. /*
  462. * Try to grab rdev->mtx. If a command is still in progress,
  463. * hopefully the driver will refuse it since it's tearing
  464. * down the device already. We wait for this command to complete
  465. * before unlinking the item from the list.
  466. * Note: as codified by the BUG_ON above we cannot get here if
  467. * a virtual interface is still present. Hence, we can only get
  468. * to lock contention here if userspace issues a command that
  469. * identified the hardware by wiphy index.
  470. */
  471. cfg80211_lock_rdev(rdev);
  472. /* nothing */
  473. cfg80211_unlock_rdev(rdev);
  474. /* If this device got a regulatory hint tell core its
  475. * free to listen now to a new shiny device regulatory hint */
  476. reg_device_remove(wiphy);
  477. cfg80211_rdev_list_generation++;
  478. device_del(&rdev->wiphy.dev);
  479. mutex_unlock(&cfg80211_mutex);
  480. flush_work(&rdev->scan_done_wk);
  481. cancel_work_sync(&rdev->conn_work);
  482. flush_work(&rdev->event_work);
  483. }
  484. EXPORT_SYMBOL(wiphy_unregister);
  485. void cfg80211_dev_free(struct cfg80211_registered_device *rdev)
  486. {
  487. struct cfg80211_internal_bss *scan, *tmp;
  488. rfkill_destroy(rdev->rfkill);
  489. mutex_destroy(&rdev->mtx);
  490. mutex_destroy(&rdev->devlist_mtx);
  491. list_for_each_entry_safe(scan, tmp, &rdev->bss_list, list)
  492. cfg80211_put_bss(&scan->pub);
  493. kfree(rdev);
  494. }
  495. void wiphy_free(struct wiphy *wiphy)
  496. {
  497. put_device(&wiphy->dev);
  498. }
  499. EXPORT_SYMBOL(wiphy_free);
  500. void wiphy_rfkill_set_hw_state(struct wiphy *wiphy, bool blocked)
  501. {
  502. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  503. if (rfkill_set_hw_state(rdev->rfkill, blocked))
  504. schedule_work(&rdev->rfkill_sync);
  505. }
  506. EXPORT_SYMBOL(wiphy_rfkill_set_hw_state);
  507. static void wdev_cleanup_work(struct work_struct *work)
  508. {
  509. struct wireless_dev *wdev;
  510. struct cfg80211_registered_device *rdev;
  511. wdev = container_of(work, struct wireless_dev, cleanup_work);
  512. rdev = wiphy_to_dev(wdev->wiphy);
  513. cfg80211_lock_rdev(rdev);
  514. if (WARN_ON(rdev->scan_req && rdev->scan_req->dev == wdev->netdev)) {
  515. rdev->scan_req->aborted = true;
  516. ___cfg80211_scan_done(rdev, true);
  517. }
  518. cfg80211_unlock_rdev(rdev);
  519. mutex_lock(&rdev->devlist_mtx);
  520. rdev->opencount--;
  521. mutex_unlock(&rdev->devlist_mtx);
  522. wake_up(&rdev->dev_wait);
  523. dev_put(wdev->netdev);
  524. }
  525. static struct device_type wiphy_type = {
  526. .name = "wlan",
  527. };
  528. static int cfg80211_netdev_notifier_call(struct notifier_block * nb,
  529. unsigned long state,
  530. void *ndev)
  531. {
  532. struct net_device *dev = ndev;
  533. struct wireless_dev *wdev = dev->ieee80211_ptr;
  534. struct cfg80211_registered_device *rdev;
  535. if (!wdev)
  536. return NOTIFY_DONE;
  537. rdev = wiphy_to_dev(wdev->wiphy);
  538. WARN_ON(wdev->iftype == NL80211_IFTYPE_UNSPECIFIED);
  539. switch (state) {
  540. case NETDEV_POST_INIT:
  541. SET_NETDEV_DEVTYPE(dev, &wiphy_type);
  542. break;
  543. case NETDEV_REGISTER:
  544. /*
  545. * NB: cannot take rdev->mtx here because this may be
  546. * called within code protected by it when interfaces
  547. * are added with nl80211.
  548. */
  549. mutex_init(&wdev->mtx);
  550. INIT_WORK(&wdev->cleanup_work, wdev_cleanup_work);
  551. INIT_LIST_HEAD(&wdev->event_list);
  552. spin_lock_init(&wdev->event_lock);
  553. INIT_LIST_HEAD(&wdev->mgmt_registrations);
  554. spin_lock_init(&wdev->mgmt_registrations_lock);
  555. mutex_lock(&rdev->devlist_mtx);
  556. list_add_rcu(&wdev->list, &rdev->netdev_list);
  557. rdev->devlist_generation++;
  558. /* can only change netns with wiphy */
  559. dev->features |= NETIF_F_NETNS_LOCAL;
  560. if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj,
  561. "phy80211")) {
  562. printk(KERN_ERR "wireless: failed to add phy80211 "
  563. "symlink to netdev!\n");
  564. }
  565. wdev->netdev = dev;
  566. wdev->sme_state = CFG80211_SME_IDLE;
  567. mutex_unlock(&rdev->devlist_mtx);
  568. #ifdef CONFIG_CFG80211_WEXT
  569. wdev->wext.default_key = -1;
  570. wdev->wext.default_mgmt_key = -1;
  571. wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
  572. #endif
  573. if (wdev->wiphy->flags & WIPHY_FLAG_PS_ON_BY_DEFAULT)
  574. wdev->ps = true;
  575. else
  576. wdev->ps = false;
  577. /* allow mac80211 to determine the timeout */
  578. wdev->ps_timeout = -1;
  579. if (rdev->ops->set_power_mgmt)
  580. if (rdev->ops->set_power_mgmt(wdev->wiphy, dev,
  581. wdev->ps,
  582. wdev->ps_timeout)) {
  583. /* assume this means it's off */
  584. wdev->ps = false;
  585. }
  586. if (!dev->ethtool_ops)
  587. dev->ethtool_ops = &cfg80211_ethtool_ops;
  588. if ((wdev->iftype == NL80211_IFTYPE_STATION ||
  589. wdev->iftype == NL80211_IFTYPE_P2P_CLIENT ||
  590. wdev->iftype == NL80211_IFTYPE_ADHOC) && !wdev->use_4addr)
  591. dev->priv_flags |= IFF_DONT_BRIDGE;
  592. break;
  593. case NETDEV_GOING_DOWN:
  594. switch (wdev->iftype) {
  595. case NL80211_IFTYPE_ADHOC:
  596. cfg80211_leave_ibss(rdev, dev, true);
  597. break;
  598. case NL80211_IFTYPE_P2P_CLIENT:
  599. case NL80211_IFTYPE_STATION:
  600. wdev_lock(wdev);
  601. #ifdef CONFIG_CFG80211_WEXT
  602. kfree(wdev->wext.ie);
  603. wdev->wext.ie = NULL;
  604. wdev->wext.ie_len = 0;
  605. wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
  606. #endif
  607. __cfg80211_disconnect(rdev, dev,
  608. WLAN_REASON_DEAUTH_LEAVING, true);
  609. cfg80211_mlme_down(rdev, dev);
  610. wdev_unlock(wdev);
  611. break;
  612. default:
  613. break;
  614. }
  615. break;
  616. case NETDEV_DOWN:
  617. dev_hold(dev);
  618. queue_work(cfg80211_wq, &wdev->cleanup_work);
  619. break;
  620. case NETDEV_UP:
  621. /*
  622. * If we have a really quick DOWN/UP succession we may
  623. * have this work still pending ... cancel it and see
  624. * if it was pending, in which case we need to account
  625. * for some of the work it would have done.
  626. */
  627. if (cancel_work_sync(&wdev->cleanup_work)) {
  628. mutex_lock(&rdev->devlist_mtx);
  629. rdev->opencount--;
  630. mutex_unlock(&rdev->devlist_mtx);
  631. dev_put(dev);
  632. }
  633. cfg80211_lock_rdev(rdev);
  634. mutex_lock(&rdev->devlist_mtx);
  635. #ifdef CONFIG_CFG80211_WEXT
  636. wdev_lock(wdev);
  637. switch (wdev->iftype) {
  638. case NL80211_IFTYPE_ADHOC:
  639. cfg80211_ibss_wext_join(rdev, wdev);
  640. break;
  641. case NL80211_IFTYPE_STATION:
  642. cfg80211_mgd_wext_connect(rdev, wdev);
  643. break;
  644. default:
  645. break;
  646. }
  647. wdev_unlock(wdev);
  648. #endif
  649. rdev->opencount++;
  650. mutex_unlock(&rdev->devlist_mtx);
  651. cfg80211_unlock_rdev(rdev);
  652. break;
  653. case NETDEV_UNREGISTER:
  654. /*
  655. * NB: cannot take rdev->mtx here because this may be
  656. * called within code protected by it when interfaces
  657. * are removed with nl80211.
  658. */
  659. mutex_lock(&rdev->devlist_mtx);
  660. /*
  661. * It is possible to get NETDEV_UNREGISTER
  662. * multiple times. To detect that, check
  663. * that the interface is still on the list
  664. * of registered interfaces, and only then
  665. * remove and clean it up.
  666. */
  667. if (!list_empty(&wdev->list)) {
  668. sysfs_remove_link(&dev->dev.kobj, "phy80211");
  669. list_del_rcu(&wdev->list);
  670. rdev->devlist_generation++;
  671. cfg80211_mlme_purge_registrations(wdev);
  672. #ifdef CONFIG_CFG80211_WEXT
  673. kfree(wdev->wext.keys);
  674. #endif
  675. }
  676. mutex_unlock(&rdev->devlist_mtx);
  677. /*
  678. * synchronise (so that we won't find this netdev
  679. * from other code any more) and then clear the list
  680. * head so that the above code can safely check for
  681. * !list_empty() to avoid double-cleanup.
  682. */
  683. synchronize_rcu();
  684. INIT_LIST_HEAD(&wdev->list);
  685. break;
  686. case NETDEV_PRE_UP:
  687. if (!(wdev->wiphy->interface_modes & BIT(wdev->iftype)))
  688. return notifier_from_errno(-EOPNOTSUPP);
  689. if (rfkill_blocked(rdev->rfkill))
  690. return notifier_from_errno(-ERFKILL);
  691. break;
  692. }
  693. return NOTIFY_DONE;
  694. }
  695. static struct notifier_block cfg80211_netdev_notifier = {
  696. .notifier_call = cfg80211_netdev_notifier_call,
  697. };
  698. static void __net_exit cfg80211_pernet_exit(struct net *net)
  699. {
  700. struct cfg80211_registered_device *rdev;
  701. rtnl_lock();
  702. mutex_lock(&cfg80211_mutex);
  703. list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
  704. if (net_eq(wiphy_net(&rdev->wiphy), net))
  705. WARN_ON(cfg80211_switch_netns(rdev, &init_net));
  706. }
  707. mutex_unlock(&cfg80211_mutex);
  708. rtnl_unlock();
  709. }
  710. static struct pernet_operations cfg80211_pernet_ops = {
  711. .exit = cfg80211_pernet_exit,
  712. };
  713. static int __init cfg80211_init(void)
  714. {
  715. int err;
  716. err = register_pernet_device(&cfg80211_pernet_ops);
  717. if (err)
  718. goto out_fail_pernet;
  719. err = wiphy_sysfs_init();
  720. if (err)
  721. goto out_fail_sysfs;
  722. err = register_netdevice_notifier(&cfg80211_netdev_notifier);
  723. if (err)
  724. goto out_fail_notifier;
  725. err = nl80211_init();
  726. if (err)
  727. goto out_fail_nl80211;
  728. ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
  729. err = regulatory_init();
  730. if (err)
  731. goto out_fail_reg;
  732. cfg80211_wq = create_singlethread_workqueue("cfg80211");
  733. if (!cfg80211_wq)
  734. goto out_fail_wq;
  735. return 0;
  736. out_fail_wq:
  737. regulatory_exit();
  738. out_fail_reg:
  739. debugfs_remove(ieee80211_debugfs_dir);
  740. out_fail_nl80211:
  741. unregister_netdevice_notifier(&cfg80211_netdev_notifier);
  742. out_fail_notifier:
  743. wiphy_sysfs_exit();
  744. out_fail_sysfs:
  745. unregister_pernet_device(&cfg80211_pernet_ops);
  746. out_fail_pernet:
  747. return err;
  748. }
  749. subsys_initcall(cfg80211_init);
  750. static void __exit cfg80211_exit(void)
  751. {
  752. debugfs_remove(ieee80211_debugfs_dir);
  753. nl80211_exit();
  754. unregister_netdevice_notifier(&cfg80211_netdev_notifier);
  755. wiphy_sysfs_exit();
  756. regulatory_exit();
  757. unregister_pernet_device(&cfg80211_pernet_ops);
  758. destroy_workqueue(cfg80211_wq);
  759. }
  760. module_exit(cfg80211_exit);