core.c 22 KB

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