core.c 21 KB

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