core.c 23 KB

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