core.c 20 KB

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