core.c 23 KB

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