core.c 20 KB

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