core.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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. #include "wext-compat.h"
  22. /* name for sysfs, %d is appended */
  23. #define PHY_NAME "phy"
  24. MODULE_AUTHOR("Johannes Berg");
  25. MODULE_LICENSE("GPL");
  26. MODULE_DESCRIPTION("wireless configuration support");
  27. /* RCU might be appropriate here since we usually
  28. * only read the list, and that can happen quite
  29. * often because we need to do it for each command */
  30. LIST_HEAD(cfg80211_rdev_list);
  31. int cfg80211_rdev_list_generation;
  32. /*
  33. * This is used to protect the cfg80211_rdev_list
  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(genl_info_net(info), 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(struct net *net, 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(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. int cfg80211_switch_netns(struct cfg80211_registered_device *rdev,
  185. struct net *net)
  186. {
  187. struct wireless_dev *wdev;
  188. int err = 0;
  189. if (!rdev->wiphy.netnsok)
  190. return -EOPNOTSUPP;
  191. list_for_each_entry(wdev, &rdev->netdev_list, list) {
  192. wdev->netdev->features &= ~NETIF_F_NETNS_LOCAL;
  193. err = dev_change_net_namespace(wdev->netdev, net, "wlan%d");
  194. if (err)
  195. break;
  196. wdev->netdev->features |= NETIF_F_NETNS_LOCAL;
  197. }
  198. if (err) {
  199. /* failed -- clean up to old netns */
  200. net = wiphy_net(&rdev->wiphy);
  201. list_for_each_entry_continue_reverse(wdev, &rdev->netdev_list,
  202. list) {
  203. wdev->netdev->features &= ~NETIF_F_NETNS_LOCAL;
  204. err = dev_change_net_namespace(wdev->netdev, net,
  205. "wlan%d");
  206. WARN_ON(err);
  207. wdev->netdev->features |= NETIF_F_NETNS_LOCAL;
  208. }
  209. }
  210. wiphy_net_set(&rdev->wiphy, net);
  211. return err;
  212. }
  213. static void cfg80211_rfkill_poll(struct rfkill *rfkill, void *data)
  214. {
  215. struct cfg80211_registered_device *rdev = data;
  216. rdev->ops->rfkill_poll(&rdev->wiphy);
  217. }
  218. static int cfg80211_rfkill_set_block(void *data, bool blocked)
  219. {
  220. struct cfg80211_registered_device *rdev = data;
  221. struct wireless_dev *wdev;
  222. if (!blocked)
  223. return 0;
  224. rtnl_lock();
  225. mutex_lock(&rdev->devlist_mtx);
  226. list_for_each_entry(wdev, &rdev->netdev_list, list)
  227. dev_close(wdev->netdev);
  228. mutex_unlock(&rdev->devlist_mtx);
  229. rtnl_unlock();
  230. return 0;
  231. }
  232. static void cfg80211_rfkill_sync_work(struct work_struct *work)
  233. {
  234. struct cfg80211_registered_device *rdev;
  235. rdev = container_of(work, struct cfg80211_registered_device, rfkill_sync);
  236. cfg80211_rfkill_set_block(rdev, rfkill_blocked(rdev->rfkill));
  237. }
  238. static void cfg80211_process_events(struct wireless_dev *wdev)
  239. {
  240. struct cfg80211_event *ev;
  241. unsigned long flags;
  242. spin_lock_irqsave(&wdev->event_lock, flags);
  243. while (!list_empty(&wdev->event_list)) {
  244. ev = list_first_entry(&wdev->event_list,
  245. struct cfg80211_event, list);
  246. list_del(&ev->list);
  247. spin_unlock_irqrestore(&wdev->event_lock, flags);
  248. wdev_lock(wdev);
  249. switch (ev->type) {
  250. case EVENT_CONNECT_RESULT:
  251. __cfg80211_connect_result(
  252. wdev->netdev, ev->cr.bssid,
  253. ev->cr.req_ie, ev->cr.req_ie_len,
  254. ev->cr.resp_ie, ev->cr.resp_ie_len,
  255. ev->cr.status,
  256. ev->cr.status == WLAN_STATUS_SUCCESS,
  257. NULL);
  258. break;
  259. case EVENT_ROAMED:
  260. __cfg80211_roamed(wdev, ev->rm.bssid,
  261. ev->rm.req_ie, ev->rm.req_ie_len,
  262. ev->rm.resp_ie, ev->rm.resp_ie_len);
  263. break;
  264. case EVENT_DISCONNECTED:
  265. __cfg80211_disconnected(wdev->netdev,
  266. ev->dc.ie, ev->dc.ie_len,
  267. ev->dc.reason, true);
  268. break;
  269. case EVENT_IBSS_JOINED:
  270. __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid);
  271. break;
  272. }
  273. wdev_unlock(wdev);
  274. kfree(ev);
  275. spin_lock_irqsave(&wdev->event_lock, flags);
  276. }
  277. spin_unlock_irqrestore(&wdev->event_lock, flags);
  278. }
  279. static void cfg80211_event_work(struct work_struct *work)
  280. {
  281. struct cfg80211_registered_device *rdev;
  282. struct wireless_dev *wdev;
  283. rdev = container_of(work, struct cfg80211_registered_device,
  284. event_work);
  285. rtnl_lock();
  286. cfg80211_lock_rdev(rdev);
  287. mutex_lock(&rdev->devlist_mtx);
  288. list_for_each_entry(wdev, &rdev->netdev_list, list)
  289. cfg80211_process_events(wdev);
  290. mutex_unlock(&rdev->devlist_mtx);
  291. cfg80211_unlock_rdev(rdev);
  292. rtnl_unlock();
  293. }
  294. /* exported functions */
  295. struct wiphy *wiphy_new(const struct cfg80211_ops *ops, int sizeof_priv)
  296. {
  297. static int wiphy_counter;
  298. struct cfg80211_registered_device *rdev;
  299. int alloc_size;
  300. WARN_ON(ops->add_key && (!ops->del_key || !ops->set_default_key));
  301. WARN_ON(ops->auth && (!ops->assoc || !ops->deauth || !ops->disassoc));
  302. WARN_ON(ops->connect && !ops->disconnect);
  303. WARN_ON(ops->join_ibss && !ops->leave_ibss);
  304. WARN_ON(ops->add_virtual_intf && !ops->del_virtual_intf);
  305. WARN_ON(ops->add_station && !ops->del_station);
  306. WARN_ON(ops->add_mpath && !ops->del_mpath);
  307. alloc_size = sizeof(*rdev) + sizeof_priv;
  308. rdev = kzalloc(alloc_size, GFP_KERNEL);
  309. if (!rdev)
  310. return NULL;
  311. rdev->ops = ops;
  312. mutex_lock(&cfg80211_mutex);
  313. rdev->wiphy_idx = wiphy_counter++;
  314. if (unlikely(!wiphy_idx_valid(rdev->wiphy_idx))) {
  315. wiphy_counter--;
  316. mutex_unlock(&cfg80211_mutex);
  317. /* ugh, wrapped! */
  318. kfree(rdev);
  319. return NULL;
  320. }
  321. mutex_unlock(&cfg80211_mutex);
  322. /* give it a proper name */
  323. dev_set_name(&rdev->wiphy.dev, PHY_NAME "%d", rdev->wiphy_idx);
  324. mutex_init(&rdev->mtx);
  325. mutex_init(&rdev->devlist_mtx);
  326. INIT_LIST_HEAD(&rdev->netdev_list);
  327. spin_lock_init(&rdev->bss_lock);
  328. INIT_LIST_HEAD(&rdev->bss_list);
  329. INIT_WORK(&rdev->scan_done_wk, __cfg80211_scan_done);
  330. device_initialize(&rdev->wiphy.dev);
  331. rdev->wiphy.dev.class = &ieee80211_class;
  332. rdev->wiphy.dev.platform_data = rdev;
  333. rdev->wiphy.ps_default = CONFIG_CFG80211_DEFAULT_PS_VALUE;
  334. wiphy_net_set(&rdev->wiphy, &init_net);
  335. rdev->rfkill_ops.set_block = cfg80211_rfkill_set_block;
  336. rdev->rfkill = rfkill_alloc(dev_name(&rdev->wiphy.dev),
  337. &rdev->wiphy.dev, RFKILL_TYPE_WLAN,
  338. &rdev->rfkill_ops, rdev);
  339. if (!rdev->rfkill) {
  340. kfree(rdev);
  341. return NULL;
  342. }
  343. INIT_WORK(&rdev->rfkill_sync, cfg80211_rfkill_sync_work);
  344. INIT_WORK(&rdev->conn_work, cfg80211_conn_work);
  345. INIT_WORK(&rdev->event_work, cfg80211_event_work);
  346. init_waitqueue_head(&rdev->dev_wait);
  347. /*
  348. * Initialize wiphy parameters to IEEE 802.11 MIB default values.
  349. * Fragmentation and RTS threshold are disabled by default with the
  350. * special -1 value.
  351. */
  352. rdev->wiphy.retry_short = 7;
  353. rdev->wiphy.retry_long = 4;
  354. rdev->wiphy.frag_threshold = (u32) -1;
  355. rdev->wiphy.rts_threshold = (u32) -1;
  356. return &rdev->wiphy;
  357. }
  358. EXPORT_SYMBOL(wiphy_new);
  359. int wiphy_register(struct wiphy *wiphy)
  360. {
  361. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  362. int res;
  363. enum ieee80211_band band;
  364. struct ieee80211_supported_band *sband;
  365. bool have_band = false;
  366. int i;
  367. u16 ifmodes = wiphy->interface_modes;
  368. /* sanity check ifmodes */
  369. WARN_ON(!ifmodes);
  370. ifmodes &= ((1 << __NL80211_IFTYPE_AFTER_LAST) - 1) & ~1;
  371. if (WARN_ON(ifmodes != wiphy->interface_modes))
  372. wiphy->interface_modes = ifmodes;
  373. /* sanity check supported bands/channels */
  374. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  375. sband = wiphy->bands[band];
  376. if (!sband)
  377. continue;
  378. sband->band = band;
  379. if (WARN_ON(!sband->n_channels || !sband->n_bitrates))
  380. return -EINVAL;
  381. /*
  382. * Since we use a u32 for rate bitmaps in
  383. * ieee80211_get_response_rate, we cannot
  384. * have more than 32 legacy rates.
  385. */
  386. if (WARN_ON(sband->n_bitrates > 32))
  387. return -EINVAL;
  388. for (i = 0; i < sband->n_channels; i++) {
  389. sband->channels[i].orig_flags =
  390. sband->channels[i].flags;
  391. sband->channels[i].orig_mag =
  392. sband->channels[i].max_antenna_gain;
  393. sband->channels[i].orig_mpwr =
  394. sband->channels[i].max_power;
  395. sband->channels[i].band = band;
  396. }
  397. have_band = true;
  398. }
  399. if (!have_band) {
  400. WARN_ON(1);
  401. return -EINVAL;
  402. }
  403. /* check and set up bitrates */
  404. ieee80211_set_bitrate_flags(wiphy);
  405. res = device_add(&rdev->wiphy.dev);
  406. if (res)
  407. return res;
  408. res = rfkill_register(rdev->rfkill);
  409. if (res)
  410. goto out_rm_dev;
  411. mutex_lock(&cfg80211_mutex);
  412. /* set up regulatory info */
  413. wiphy_update_regulatory(wiphy, NL80211_REGDOM_SET_BY_CORE);
  414. list_add(&rdev->list, &cfg80211_rdev_list);
  415. cfg80211_rdev_list_generation++;
  416. mutex_unlock(&cfg80211_mutex);
  417. /* add to debugfs */
  418. rdev->wiphy.debugfsdir =
  419. debugfs_create_dir(wiphy_name(&rdev->wiphy),
  420. ieee80211_debugfs_dir);
  421. if (IS_ERR(rdev->wiphy.debugfsdir))
  422. rdev->wiphy.debugfsdir = NULL;
  423. if (wiphy->custom_regulatory) {
  424. struct regulatory_request request;
  425. request.wiphy_idx = get_wiphy_idx(wiphy);
  426. request.initiator = NL80211_REGDOM_SET_BY_DRIVER;
  427. request.alpha2[0] = '9';
  428. request.alpha2[1] = '9';
  429. nl80211_send_reg_change_event(&request);
  430. }
  431. cfg80211_debugfs_rdev_add(rdev);
  432. return 0;
  433. out_rm_dev:
  434. device_del(&rdev->wiphy.dev);
  435. return res;
  436. }
  437. EXPORT_SYMBOL(wiphy_register);
  438. void wiphy_rfkill_start_polling(struct wiphy *wiphy)
  439. {
  440. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  441. if (!rdev->ops->rfkill_poll)
  442. return;
  443. rdev->rfkill_ops.poll = cfg80211_rfkill_poll;
  444. rfkill_resume_polling(rdev->rfkill);
  445. }
  446. EXPORT_SYMBOL(wiphy_rfkill_start_polling);
  447. void wiphy_rfkill_stop_polling(struct wiphy *wiphy)
  448. {
  449. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  450. rfkill_pause_polling(rdev->rfkill);
  451. }
  452. EXPORT_SYMBOL(wiphy_rfkill_stop_polling);
  453. void wiphy_unregister(struct wiphy *wiphy)
  454. {
  455. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  456. rfkill_unregister(rdev->rfkill);
  457. /* protect the device list */
  458. mutex_lock(&cfg80211_mutex);
  459. wait_event(rdev->dev_wait, ({
  460. int __count;
  461. mutex_lock(&rdev->devlist_mtx);
  462. __count = rdev->opencount;
  463. mutex_unlock(&rdev->devlist_mtx);
  464. __count == 0;}));
  465. mutex_lock(&rdev->devlist_mtx);
  466. BUG_ON(!list_empty(&rdev->netdev_list));
  467. mutex_unlock(&rdev->devlist_mtx);
  468. /*
  469. * First remove the hardware from everywhere, this makes
  470. * it impossible to find from userspace.
  471. */
  472. cfg80211_debugfs_rdev_del(rdev);
  473. list_del(&rdev->list);
  474. /*
  475. * Try to grab rdev->mtx. If a command is still in progress,
  476. * hopefully the driver will refuse it since it's tearing
  477. * down the device already. We wait for this command to complete
  478. * before unlinking the item from the list.
  479. * Note: as codified by the BUG_ON above we cannot get here if
  480. * a virtual interface is still present. Hence, we can only get
  481. * to lock contention here if userspace issues a command that
  482. * identified the hardware by wiphy index.
  483. */
  484. cfg80211_lock_rdev(rdev);
  485. /* nothing */
  486. cfg80211_unlock_rdev(rdev);
  487. /* If this device got a regulatory hint tell core its
  488. * free to listen now to a new shiny device regulatory hint */
  489. reg_device_remove(wiphy);
  490. cfg80211_rdev_list_generation++;
  491. device_del(&rdev->wiphy.dev);
  492. debugfs_remove(rdev->wiphy.debugfsdir);
  493. mutex_unlock(&cfg80211_mutex);
  494. flush_work(&rdev->scan_done_wk);
  495. cancel_work_sync(&rdev->conn_work);
  496. flush_work(&rdev->event_work);
  497. }
  498. EXPORT_SYMBOL(wiphy_unregister);
  499. void cfg80211_dev_free(struct cfg80211_registered_device *rdev)
  500. {
  501. struct cfg80211_internal_bss *scan, *tmp;
  502. rfkill_destroy(rdev->rfkill);
  503. mutex_destroy(&rdev->mtx);
  504. mutex_destroy(&rdev->devlist_mtx);
  505. list_for_each_entry_safe(scan, tmp, &rdev->bss_list, list)
  506. cfg80211_put_bss(&scan->pub);
  507. kfree(rdev);
  508. }
  509. void wiphy_free(struct wiphy *wiphy)
  510. {
  511. put_device(&wiphy->dev);
  512. }
  513. EXPORT_SYMBOL(wiphy_free);
  514. void wiphy_rfkill_set_hw_state(struct wiphy *wiphy, bool blocked)
  515. {
  516. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  517. if (rfkill_set_hw_state(rdev->rfkill, blocked))
  518. schedule_work(&rdev->rfkill_sync);
  519. }
  520. EXPORT_SYMBOL(wiphy_rfkill_set_hw_state);
  521. static void wdev_cleanup_work(struct work_struct *work)
  522. {
  523. struct wireless_dev *wdev;
  524. struct cfg80211_registered_device *rdev;
  525. wdev = container_of(work, struct wireless_dev, cleanup_work);
  526. rdev = wiphy_to_dev(wdev->wiphy);
  527. cfg80211_lock_rdev(rdev);
  528. if (WARN_ON(rdev->scan_req && rdev->scan_req->dev == wdev->netdev)) {
  529. rdev->scan_req->aborted = true;
  530. ___cfg80211_scan_done(rdev);
  531. }
  532. cfg80211_unlock_rdev(rdev);
  533. mutex_lock(&rdev->devlist_mtx);
  534. rdev->opencount--;
  535. mutex_unlock(&rdev->devlist_mtx);
  536. wake_up(&rdev->dev_wait);
  537. dev_put(wdev->netdev);
  538. }
  539. static int cfg80211_netdev_notifier_call(struct notifier_block * nb,
  540. unsigned long state,
  541. void *ndev)
  542. {
  543. struct net_device *dev = ndev;
  544. struct wireless_dev *wdev = dev->ieee80211_ptr;
  545. struct cfg80211_registered_device *rdev;
  546. if (!wdev)
  547. return NOTIFY_DONE;
  548. rdev = wiphy_to_dev(wdev->wiphy);
  549. WARN_ON(wdev->iftype == NL80211_IFTYPE_UNSPECIFIED);
  550. switch (state) {
  551. case NETDEV_REGISTER:
  552. /*
  553. * NB: cannot take rdev->mtx here because this may be
  554. * called within code protected by it when interfaces
  555. * are added with nl80211.
  556. */
  557. mutex_init(&wdev->mtx);
  558. INIT_WORK(&wdev->cleanup_work, wdev_cleanup_work);
  559. INIT_LIST_HEAD(&wdev->event_list);
  560. spin_lock_init(&wdev->event_lock);
  561. mutex_lock(&rdev->devlist_mtx);
  562. list_add(&wdev->list, &rdev->netdev_list);
  563. rdev->devlist_generation++;
  564. /* can only change netns with wiphy */
  565. dev->features |= NETIF_F_NETNS_LOCAL;
  566. if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj,
  567. "phy80211")) {
  568. printk(KERN_ERR "wireless: failed to add phy80211 "
  569. "symlink to netdev!\n");
  570. }
  571. wdev->netdev = dev;
  572. wdev->sme_state = CFG80211_SME_IDLE;
  573. mutex_unlock(&rdev->devlist_mtx);
  574. #ifdef CONFIG_WIRELESS_EXT
  575. if (!dev->wireless_handlers)
  576. dev->wireless_handlers = &cfg80211_wext_handler;
  577. wdev->wext.default_key = -1;
  578. wdev->wext.default_mgmt_key = -1;
  579. wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
  580. wdev->wext.ps = wdev->wiphy->ps_default;
  581. wdev->wext.ps_timeout = 100;
  582. if (rdev->ops->set_power_mgmt)
  583. if (rdev->ops->set_power_mgmt(wdev->wiphy, dev,
  584. wdev->wext.ps,
  585. wdev->wext.ps_timeout)) {
  586. /* assume this means it's off */
  587. wdev->wext.ps = false;
  588. }
  589. #endif
  590. break;
  591. case NETDEV_GOING_DOWN:
  592. switch (wdev->iftype) {
  593. case NL80211_IFTYPE_ADHOC:
  594. cfg80211_leave_ibss(rdev, dev, true);
  595. break;
  596. case NL80211_IFTYPE_STATION:
  597. wdev_lock(wdev);
  598. #ifdef CONFIG_WIRELESS_EXT
  599. kfree(wdev->wext.ie);
  600. wdev->wext.ie = NULL;
  601. wdev->wext.ie_len = 0;
  602. wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
  603. #endif
  604. __cfg80211_disconnect(rdev, dev,
  605. WLAN_REASON_DEAUTH_LEAVING, true);
  606. cfg80211_mlme_down(rdev, dev);
  607. wdev_unlock(wdev);
  608. break;
  609. default:
  610. break;
  611. }
  612. dev_hold(dev);
  613. schedule_work(&wdev->cleanup_work);
  614. break;
  615. case NETDEV_UP:
  616. /*
  617. * If we have a really quick DOWN/UP succession we may
  618. * have this work still pending ... cancel it and see
  619. * if it was pending, in which case we need to account
  620. * for some of the work it would have done.
  621. */
  622. if (cancel_work_sync(&wdev->cleanup_work)) {
  623. mutex_lock(&rdev->devlist_mtx);
  624. rdev->opencount--;
  625. mutex_unlock(&rdev->devlist_mtx);
  626. dev_put(dev);
  627. }
  628. #ifdef CONFIG_WIRELESS_EXT
  629. cfg80211_lock_rdev(rdev);
  630. mutex_lock(&rdev->devlist_mtx);
  631. wdev_lock(wdev);
  632. switch (wdev->iftype) {
  633. case NL80211_IFTYPE_ADHOC:
  634. cfg80211_ibss_wext_join(rdev, wdev);
  635. break;
  636. case NL80211_IFTYPE_STATION:
  637. cfg80211_mgd_wext_connect(rdev, wdev);
  638. break;
  639. default:
  640. break;
  641. }
  642. wdev_unlock(wdev);
  643. rdev->opencount++;
  644. mutex_unlock(&rdev->devlist_mtx);
  645. cfg80211_unlock_rdev(rdev);
  646. #endif
  647. break;
  648. case NETDEV_UNREGISTER:
  649. /*
  650. * NB: cannot take rdev->mtx here because this may be
  651. * called within code protected by it when interfaces
  652. * are removed with nl80211.
  653. */
  654. mutex_lock(&rdev->devlist_mtx);
  655. /*
  656. * It is possible to get NETDEV_UNREGISTER
  657. * multiple times. To detect that, check
  658. * that the interface is still on the list
  659. * of registered interfaces, and only then
  660. * remove and clean it up.
  661. */
  662. if (!list_empty(&wdev->list)) {
  663. sysfs_remove_link(&dev->dev.kobj, "phy80211");
  664. list_del_init(&wdev->list);
  665. rdev->devlist_generation++;
  666. #ifdef CONFIG_WIRELESS_EXT
  667. kfree(wdev->wext.keys);
  668. #endif
  669. }
  670. mutex_unlock(&rdev->devlist_mtx);
  671. break;
  672. case NETDEV_PRE_UP:
  673. if (!(wdev->wiphy->interface_modes & BIT(wdev->iftype)))
  674. return notifier_from_errno(-EOPNOTSUPP);
  675. if (rfkill_blocked(rdev->rfkill))
  676. return notifier_from_errno(-ERFKILL);
  677. break;
  678. }
  679. return NOTIFY_DONE;
  680. }
  681. static struct notifier_block cfg80211_netdev_notifier = {
  682. .notifier_call = cfg80211_netdev_notifier_call,
  683. };
  684. static void __net_exit cfg80211_pernet_exit(struct net *net)
  685. {
  686. struct cfg80211_registered_device *rdev;
  687. rtnl_lock();
  688. mutex_lock(&cfg80211_mutex);
  689. list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
  690. if (net_eq(wiphy_net(&rdev->wiphy), net))
  691. WARN_ON(cfg80211_switch_netns(rdev, &init_net));
  692. }
  693. mutex_unlock(&cfg80211_mutex);
  694. rtnl_unlock();
  695. }
  696. static struct pernet_operations cfg80211_pernet_ops = {
  697. .exit = cfg80211_pernet_exit,
  698. };
  699. static int __init cfg80211_init(void)
  700. {
  701. int err;
  702. err = register_pernet_device(&cfg80211_pernet_ops);
  703. if (err)
  704. goto out_fail_pernet;
  705. err = wiphy_sysfs_init();
  706. if (err)
  707. goto out_fail_sysfs;
  708. err = register_netdevice_notifier(&cfg80211_netdev_notifier);
  709. if (err)
  710. goto out_fail_notifier;
  711. err = nl80211_init();
  712. if (err)
  713. goto out_fail_nl80211;
  714. ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
  715. err = regulatory_init();
  716. if (err)
  717. goto out_fail_reg;
  718. return 0;
  719. out_fail_reg:
  720. debugfs_remove(ieee80211_debugfs_dir);
  721. out_fail_nl80211:
  722. unregister_netdevice_notifier(&cfg80211_netdev_notifier);
  723. out_fail_notifier:
  724. wiphy_sysfs_exit();
  725. out_fail_sysfs:
  726. unregister_pernet_device(&cfg80211_pernet_ops);
  727. out_fail_pernet:
  728. return err;
  729. }
  730. subsys_initcall(cfg80211_init);
  731. static void cfg80211_exit(void)
  732. {
  733. debugfs_remove(ieee80211_debugfs_dir);
  734. nl80211_exit();
  735. unregister_netdevice_notifier(&cfg80211_netdev_notifier);
  736. wiphy_sysfs_exit();
  737. regulatory_exit();
  738. unregister_pernet_device(&cfg80211_pernet_ops);
  739. }
  740. module_exit(cfg80211_exit);