core.c 18 KB

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