core.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  1. /*
  2. * This is the linux wireless configuration interface.
  3. *
  4. * Copyright 2006-2010 Johannes Berg <[email protected]>
  5. * Copyright 2013-2014 Intel Mobile Communications GmbH
  6. * Copyright 2015 Intel Deutschland GmbH
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/if.h>
  10. #include <linux/module.h>
  11. #include <linux/err.h>
  12. #include <linux/list.h>
  13. #include <linux/slab.h>
  14. #include <linux/nl80211.h>
  15. #include <linux/debugfs.h>
  16. #include <linux/notifier.h>
  17. #include <linux/device.h>
  18. #include <linux/etherdevice.h>
  19. #include <linux/rtnetlink.h>
  20. #include <linux/sched.h>
  21. #include <net/genetlink.h>
  22. #include <net/cfg80211.h>
  23. #include "nl80211.h"
  24. #include "core.h"
  25. #include "sysfs.h"
  26. #include "debugfs.h"
  27. #include "wext-compat.h"
  28. #include "rdev-ops.h"
  29. /* name for sysfs, %d is appended */
  30. #define PHY_NAME "phy"
  31. MODULE_AUTHOR("Johannes Berg");
  32. MODULE_LICENSE("GPL");
  33. MODULE_DESCRIPTION("wireless configuration support");
  34. MODULE_ALIAS_GENL_FAMILY(NL80211_GENL_NAME);
  35. /* RCU-protected (and RTNL for writers) */
  36. LIST_HEAD(cfg80211_rdev_list);
  37. int cfg80211_rdev_list_generation;
  38. /* for debugfs */
  39. static struct dentry *ieee80211_debugfs_dir;
  40. /* for the cleanup, scan and event works */
  41. struct workqueue_struct *cfg80211_wq;
  42. static bool cfg80211_disable_40mhz_24ghz;
  43. module_param(cfg80211_disable_40mhz_24ghz, bool, 0644);
  44. MODULE_PARM_DESC(cfg80211_disable_40mhz_24ghz,
  45. "Disable 40MHz support in the 2.4GHz band");
  46. struct cfg80211_registered_device *cfg80211_rdev_by_wiphy_idx(int wiphy_idx)
  47. {
  48. struct cfg80211_registered_device *result = NULL, *rdev;
  49. ASSERT_RTNL();
  50. list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
  51. if (rdev->wiphy_idx == wiphy_idx) {
  52. result = rdev;
  53. break;
  54. }
  55. }
  56. return result;
  57. }
  58. int get_wiphy_idx(struct wiphy *wiphy)
  59. {
  60. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  61. return rdev->wiphy_idx;
  62. }
  63. struct wiphy *wiphy_idx_to_wiphy(int wiphy_idx)
  64. {
  65. struct cfg80211_registered_device *rdev;
  66. ASSERT_RTNL();
  67. rdev = cfg80211_rdev_by_wiphy_idx(wiphy_idx);
  68. if (!rdev)
  69. return NULL;
  70. return &rdev->wiphy;
  71. }
  72. static int cfg80211_dev_check_name(struct cfg80211_registered_device *rdev,
  73. const char *newname)
  74. {
  75. struct cfg80211_registered_device *rdev2;
  76. int wiphy_idx, taken = -1, digits;
  77. ASSERT_RTNL();
  78. if (strlen(newname) > NL80211_WIPHY_NAME_MAXLEN)
  79. return -EINVAL;
  80. /* prohibit calling the thing phy%d when %d is not its number */
  81. sscanf(newname, PHY_NAME "%d%n", &wiphy_idx, &taken);
  82. if (taken == strlen(newname) && wiphy_idx != rdev->wiphy_idx) {
  83. /* count number of places needed to print wiphy_idx */
  84. digits = 1;
  85. while (wiphy_idx /= 10)
  86. digits++;
  87. /*
  88. * deny the name if it is phy<idx> where <idx> is printed
  89. * without leading zeroes. taken == strlen(newname) here
  90. */
  91. if (taken == strlen(PHY_NAME) + digits)
  92. return -EINVAL;
  93. }
  94. /* Ensure another device does not already have this name. */
  95. list_for_each_entry(rdev2, &cfg80211_rdev_list, list)
  96. if (strcmp(newname, wiphy_name(&rdev2->wiphy)) == 0)
  97. return -EINVAL;
  98. return 0;
  99. }
  100. int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
  101. char *newname)
  102. {
  103. int result;
  104. ASSERT_RTNL();
  105. /* Ignore nop renames */
  106. if (strcmp(newname, wiphy_name(&rdev->wiphy)) == 0)
  107. return 0;
  108. result = cfg80211_dev_check_name(rdev, newname);
  109. if (result < 0)
  110. return result;
  111. result = device_rename(&rdev->wiphy.dev, newname);
  112. if (result)
  113. return result;
  114. if (rdev->wiphy.debugfsdir &&
  115. !debugfs_rename(rdev->wiphy.debugfsdir->d_parent,
  116. rdev->wiphy.debugfsdir,
  117. rdev->wiphy.debugfsdir->d_parent,
  118. newname))
  119. pr_err("failed to rename debugfs dir to %s!\n", newname);
  120. nl80211_notify_wiphy(rdev, NL80211_CMD_NEW_WIPHY);
  121. return 0;
  122. }
  123. int cfg80211_switch_netns(struct cfg80211_registered_device *rdev,
  124. struct net *net)
  125. {
  126. struct wireless_dev *wdev;
  127. int err = 0;
  128. if (!(rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK))
  129. return -EOPNOTSUPP;
  130. list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
  131. if (!wdev->netdev)
  132. continue;
  133. wdev->netdev->features &= ~NETIF_F_NETNS_LOCAL;
  134. err = dev_change_net_namespace(wdev->netdev, net, "wlan%d");
  135. if (err)
  136. break;
  137. wdev->netdev->features |= NETIF_F_NETNS_LOCAL;
  138. }
  139. if (err) {
  140. /* failed -- clean up to old netns */
  141. net = wiphy_net(&rdev->wiphy);
  142. list_for_each_entry_continue_reverse(wdev,
  143. &rdev->wiphy.wdev_list,
  144. list) {
  145. if (!wdev->netdev)
  146. continue;
  147. wdev->netdev->features &= ~NETIF_F_NETNS_LOCAL;
  148. err = dev_change_net_namespace(wdev->netdev, net,
  149. "wlan%d");
  150. WARN_ON(err);
  151. wdev->netdev->features |= NETIF_F_NETNS_LOCAL;
  152. }
  153. return err;
  154. }
  155. wiphy_net_set(&rdev->wiphy, net);
  156. err = device_rename(&rdev->wiphy.dev, dev_name(&rdev->wiphy.dev));
  157. WARN_ON(err);
  158. return 0;
  159. }
  160. static void cfg80211_rfkill_poll(struct rfkill *rfkill, void *data)
  161. {
  162. struct cfg80211_registered_device *rdev = data;
  163. rdev_rfkill_poll(rdev);
  164. }
  165. void cfg80211_stop_p2p_device(struct cfg80211_registered_device *rdev,
  166. struct wireless_dev *wdev)
  167. {
  168. ASSERT_RTNL();
  169. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_P2P_DEVICE))
  170. return;
  171. if (!wdev->p2p_started)
  172. return;
  173. rdev_stop_p2p_device(rdev, wdev);
  174. wdev->p2p_started = false;
  175. rdev->opencount--;
  176. if (rdev->scan_req && rdev->scan_req->wdev == wdev) {
  177. if (WARN_ON(!rdev->scan_req->notified))
  178. rdev->scan_req->info.aborted = true;
  179. ___cfg80211_scan_done(rdev, false);
  180. }
  181. }
  182. void cfg80211_stop_nan(struct cfg80211_registered_device *rdev,
  183. struct wireless_dev *wdev)
  184. {
  185. ASSERT_RTNL();
  186. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_NAN))
  187. return;
  188. if (!wdev->nan_started)
  189. return;
  190. rdev_stop_nan(rdev, wdev);
  191. wdev->nan_started = false;
  192. rdev->opencount--;
  193. }
  194. void cfg80211_shutdown_all_interfaces(struct wiphy *wiphy)
  195. {
  196. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  197. struct wireless_dev *wdev;
  198. ASSERT_RTNL();
  199. list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
  200. if (wdev->netdev) {
  201. dev_close(wdev->netdev);
  202. continue;
  203. }
  204. /* otherwise, check iftype */
  205. switch (wdev->iftype) {
  206. case NL80211_IFTYPE_P2P_DEVICE:
  207. cfg80211_stop_p2p_device(rdev, wdev);
  208. break;
  209. case NL80211_IFTYPE_NAN:
  210. cfg80211_stop_nan(rdev, wdev);
  211. break;
  212. default:
  213. break;
  214. }
  215. }
  216. }
  217. EXPORT_SYMBOL_GPL(cfg80211_shutdown_all_interfaces);
  218. static int cfg80211_rfkill_set_block(void *data, bool blocked)
  219. {
  220. struct cfg80211_registered_device *rdev = data;
  221. if (!blocked)
  222. return 0;
  223. rtnl_lock();
  224. cfg80211_shutdown_all_interfaces(&rdev->wiphy);
  225. rtnl_unlock();
  226. return 0;
  227. }
  228. static void cfg80211_rfkill_sync_work(struct work_struct *work)
  229. {
  230. struct cfg80211_registered_device *rdev;
  231. rdev = container_of(work, struct cfg80211_registered_device, rfkill_sync);
  232. cfg80211_rfkill_set_block(rdev, rfkill_blocked(rdev->rfkill));
  233. }
  234. static void cfg80211_event_work(struct work_struct *work)
  235. {
  236. struct cfg80211_registered_device *rdev;
  237. rdev = container_of(work, struct cfg80211_registered_device,
  238. event_work);
  239. rtnl_lock();
  240. cfg80211_process_rdev_events(rdev);
  241. rtnl_unlock();
  242. }
  243. void cfg80211_destroy_ifaces(struct cfg80211_registered_device *rdev)
  244. {
  245. struct cfg80211_iface_destroy *item;
  246. ASSERT_RTNL();
  247. spin_lock_irq(&rdev->destroy_list_lock);
  248. while ((item = list_first_entry_or_null(&rdev->destroy_list,
  249. struct cfg80211_iface_destroy,
  250. list))) {
  251. struct wireless_dev *wdev, *tmp;
  252. u32 nlportid = item->nlportid;
  253. list_del(&item->list);
  254. kfree(item);
  255. spin_unlock_irq(&rdev->destroy_list_lock);
  256. list_for_each_entry_safe(wdev, tmp,
  257. &rdev->wiphy.wdev_list, list) {
  258. if (nlportid == wdev->owner_nlportid)
  259. rdev_del_virtual_intf(rdev, wdev);
  260. }
  261. spin_lock_irq(&rdev->destroy_list_lock);
  262. }
  263. spin_unlock_irq(&rdev->destroy_list_lock);
  264. }
  265. static void cfg80211_destroy_iface_wk(struct work_struct *work)
  266. {
  267. struct cfg80211_registered_device *rdev;
  268. rdev = container_of(work, struct cfg80211_registered_device,
  269. destroy_work);
  270. rtnl_lock();
  271. cfg80211_destroy_ifaces(rdev);
  272. rtnl_unlock();
  273. }
  274. static void cfg80211_sched_scan_stop_wk(struct work_struct *work)
  275. {
  276. struct cfg80211_registered_device *rdev;
  277. rdev = container_of(work, struct cfg80211_registered_device,
  278. sched_scan_stop_wk);
  279. rtnl_lock();
  280. __cfg80211_stop_sched_scan(rdev, false);
  281. rtnl_unlock();
  282. }
  283. /* exported functions */
  284. struct wiphy *wiphy_new_nm(const struct cfg80211_ops *ops, int sizeof_priv,
  285. const char *requested_name)
  286. {
  287. static atomic_t wiphy_counter = ATOMIC_INIT(0);
  288. struct cfg80211_registered_device *rdev;
  289. int alloc_size;
  290. WARN_ON(ops->add_key && (!ops->del_key || !ops->set_default_key));
  291. WARN_ON(ops->auth && (!ops->assoc || !ops->deauth || !ops->disassoc));
  292. WARN_ON(ops->connect && !ops->disconnect);
  293. WARN_ON(ops->join_ibss && !ops->leave_ibss);
  294. WARN_ON(ops->add_virtual_intf && !ops->del_virtual_intf);
  295. WARN_ON(ops->add_station && !ops->del_station);
  296. WARN_ON(ops->add_mpath && !ops->del_mpath);
  297. WARN_ON(ops->join_mesh && !ops->leave_mesh);
  298. WARN_ON(ops->start_p2p_device && !ops->stop_p2p_device);
  299. WARN_ON(ops->start_ap && !ops->stop_ap);
  300. WARN_ON(ops->join_ocb && !ops->leave_ocb);
  301. WARN_ON(ops->suspend && !ops->resume);
  302. WARN_ON(ops->sched_scan_start && !ops->sched_scan_stop);
  303. WARN_ON(ops->remain_on_channel && !ops->cancel_remain_on_channel);
  304. WARN_ON(ops->tdls_channel_switch && !ops->tdls_cancel_channel_switch);
  305. WARN_ON(ops->add_tx_ts && !ops->del_tx_ts);
  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. rdev->wiphy_idx = atomic_inc_return(&wiphy_counter);
  312. if (unlikely(rdev->wiphy_idx < 0)) {
  313. /* ugh, wrapped! */
  314. atomic_dec(&wiphy_counter);
  315. kfree(rdev);
  316. return NULL;
  317. }
  318. /* atomic_inc_return makes it start at 1, make it start at 0 */
  319. rdev->wiphy_idx--;
  320. /* give it a proper name */
  321. if (requested_name && requested_name[0]) {
  322. int rv;
  323. rtnl_lock();
  324. rv = cfg80211_dev_check_name(rdev, requested_name);
  325. if (rv < 0) {
  326. rtnl_unlock();
  327. goto use_default_name;
  328. }
  329. rv = dev_set_name(&rdev->wiphy.dev, "%s", requested_name);
  330. rtnl_unlock();
  331. if (rv)
  332. goto use_default_name;
  333. } else {
  334. int rv;
  335. use_default_name:
  336. /* NOTE: This is *probably* safe w/out holding rtnl because of
  337. * the restrictions on phy names. Probably this call could
  338. * fail if some other part of the kernel (re)named a device
  339. * phyX. But, might should add some locking and check return
  340. * value, and use a different name if this one exists?
  341. */
  342. rv = dev_set_name(&rdev->wiphy.dev, PHY_NAME "%d", rdev->wiphy_idx);
  343. if (rv < 0) {
  344. kfree(rdev);
  345. return NULL;
  346. }
  347. }
  348. INIT_LIST_HEAD(&rdev->wiphy.wdev_list);
  349. INIT_LIST_HEAD(&rdev->beacon_registrations);
  350. spin_lock_init(&rdev->beacon_registrations_lock);
  351. spin_lock_init(&rdev->bss_lock);
  352. INIT_LIST_HEAD(&rdev->bss_list);
  353. INIT_WORK(&rdev->scan_done_wk, __cfg80211_scan_done);
  354. INIT_WORK(&rdev->sched_scan_results_wk, __cfg80211_sched_scan_results);
  355. INIT_LIST_HEAD(&rdev->mlme_unreg);
  356. spin_lock_init(&rdev->mlme_unreg_lock);
  357. INIT_WORK(&rdev->mlme_unreg_wk, cfg80211_mlme_unreg_wk);
  358. INIT_DELAYED_WORK(&rdev->dfs_update_channels_wk,
  359. cfg80211_dfs_channels_update_work);
  360. #ifdef CONFIG_CFG80211_WEXT
  361. rdev->wiphy.wext = &cfg80211_wext_handler;
  362. #endif
  363. device_initialize(&rdev->wiphy.dev);
  364. rdev->wiphy.dev.class = &ieee80211_class;
  365. rdev->wiphy.dev.platform_data = rdev;
  366. device_enable_async_suspend(&rdev->wiphy.dev);
  367. INIT_LIST_HEAD(&rdev->destroy_list);
  368. spin_lock_init(&rdev->destroy_list_lock);
  369. INIT_WORK(&rdev->destroy_work, cfg80211_destroy_iface_wk);
  370. INIT_WORK(&rdev->sched_scan_stop_wk, cfg80211_sched_scan_stop_wk);
  371. #ifdef CONFIG_CFG80211_DEFAULT_PS
  372. rdev->wiphy.flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
  373. #endif
  374. wiphy_net_set(&rdev->wiphy, &init_net);
  375. rdev->rfkill_ops.set_block = cfg80211_rfkill_set_block;
  376. rdev->rfkill = rfkill_alloc(dev_name(&rdev->wiphy.dev),
  377. &rdev->wiphy.dev, RFKILL_TYPE_WLAN,
  378. &rdev->rfkill_ops, rdev);
  379. if (!rdev->rfkill) {
  380. wiphy_free(&rdev->wiphy);
  381. return NULL;
  382. }
  383. INIT_WORK(&rdev->rfkill_sync, cfg80211_rfkill_sync_work);
  384. INIT_WORK(&rdev->conn_work, cfg80211_conn_work);
  385. INIT_WORK(&rdev->event_work, cfg80211_event_work);
  386. init_waitqueue_head(&rdev->dev_wait);
  387. /*
  388. * Initialize wiphy parameters to IEEE 802.11 MIB default values.
  389. * Fragmentation and RTS threshold are disabled by default with the
  390. * special -1 value.
  391. */
  392. rdev->wiphy.retry_short = 7;
  393. rdev->wiphy.retry_long = 4;
  394. rdev->wiphy.frag_threshold = (u32) -1;
  395. rdev->wiphy.rts_threshold = (u32) -1;
  396. rdev->wiphy.coverage_class = 0;
  397. rdev->wiphy.max_num_csa_counters = 1;
  398. rdev->wiphy.max_sched_scan_plans = 1;
  399. rdev->wiphy.max_sched_scan_plan_interval = U32_MAX;
  400. return &rdev->wiphy;
  401. }
  402. EXPORT_SYMBOL(wiphy_new_nm);
  403. static int wiphy_verify_combinations(struct wiphy *wiphy)
  404. {
  405. const struct ieee80211_iface_combination *c;
  406. int i, j;
  407. for (i = 0; i < wiphy->n_iface_combinations; i++) {
  408. u32 cnt = 0;
  409. u16 all_iftypes = 0;
  410. c = &wiphy->iface_combinations[i];
  411. /*
  412. * Combinations with just one interface aren't real,
  413. * however we make an exception for DFS.
  414. */
  415. if (WARN_ON((c->max_interfaces < 2) && !c->radar_detect_widths))
  416. return -EINVAL;
  417. /* Need at least one channel */
  418. if (WARN_ON(!c->num_different_channels))
  419. return -EINVAL;
  420. /*
  421. * Put a sane limit on maximum number of different
  422. * channels to simplify channel accounting code.
  423. */
  424. if (WARN_ON(c->num_different_channels >
  425. CFG80211_MAX_NUM_DIFFERENT_CHANNELS))
  426. return -EINVAL;
  427. /* DFS only works on one channel. */
  428. if (WARN_ON(c->radar_detect_widths &&
  429. (c->num_different_channels > 1)))
  430. return -EINVAL;
  431. if (WARN_ON(!c->n_limits))
  432. return -EINVAL;
  433. for (j = 0; j < c->n_limits; j++) {
  434. u16 types = c->limits[j].types;
  435. /* interface types shouldn't overlap */
  436. if (WARN_ON(types & all_iftypes))
  437. return -EINVAL;
  438. all_iftypes |= types;
  439. if (WARN_ON(!c->limits[j].max))
  440. return -EINVAL;
  441. /* Shouldn't list software iftypes in combinations! */
  442. if (WARN_ON(wiphy->software_iftypes & types))
  443. return -EINVAL;
  444. /* Only a single P2P_DEVICE can be allowed */
  445. if (WARN_ON(types & BIT(NL80211_IFTYPE_P2P_DEVICE) &&
  446. c->limits[j].max > 1))
  447. return -EINVAL;
  448. /* Only a single NAN can be allowed */
  449. if (WARN_ON(types & BIT(NL80211_IFTYPE_NAN) &&
  450. c->limits[j].max > 1))
  451. return -EINVAL;
  452. cnt += c->limits[j].max;
  453. /*
  454. * Don't advertise an unsupported type
  455. * in a combination.
  456. */
  457. if (WARN_ON((wiphy->interface_modes & types) != types))
  458. return -EINVAL;
  459. }
  460. /* You can't even choose that many! */
  461. if (WARN_ON(cnt < c->max_interfaces))
  462. return -EINVAL;
  463. }
  464. return 0;
  465. }
  466. int wiphy_register(struct wiphy *wiphy)
  467. {
  468. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  469. int res;
  470. enum nl80211_band band;
  471. struct ieee80211_supported_band *sband;
  472. bool have_band = false;
  473. int i;
  474. u16 ifmodes = wiphy->interface_modes;
  475. #ifdef CONFIG_PM
  476. if (WARN_ON(wiphy->wowlan &&
  477. (wiphy->wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
  478. !(wiphy->wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY)))
  479. return -EINVAL;
  480. if (WARN_ON(wiphy->wowlan &&
  481. !wiphy->wowlan->flags && !wiphy->wowlan->n_patterns &&
  482. !wiphy->wowlan->tcp))
  483. return -EINVAL;
  484. #endif
  485. if (WARN_ON((wiphy->features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH) &&
  486. (!rdev->ops->tdls_channel_switch ||
  487. !rdev->ops->tdls_cancel_channel_switch)))
  488. return -EINVAL;
  489. if (WARN_ON((wiphy->interface_modes & BIT(NL80211_IFTYPE_NAN)) &&
  490. (!rdev->ops->start_nan || !rdev->ops->stop_nan ||
  491. !rdev->ops->add_nan_func || !rdev->ops->del_nan_func)))
  492. return -EINVAL;
  493. /*
  494. * if a wiphy has unsupported modes for regulatory channel enforcement,
  495. * opt-out of enforcement checking
  496. */
  497. if (wiphy->interface_modes & ~(BIT(NL80211_IFTYPE_STATION) |
  498. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  499. BIT(NL80211_IFTYPE_AP) |
  500. BIT(NL80211_IFTYPE_P2P_GO) |
  501. BIT(NL80211_IFTYPE_ADHOC) |
  502. BIT(NL80211_IFTYPE_P2P_DEVICE) |
  503. BIT(NL80211_IFTYPE_NAN) |
  504. BIT(NL80211_IFTYPE_AP_VLAN) |
  505. BIT(NL80211_IFTYPE_MONITOR)))
  506. wiphy->regulatory_flags |= REGULATORY_IGNORE_STALE_KICKOFF;
  507. if (WARN_ON((wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) &&
  508. (wiphy->regulatory_flags &
  509. (REGULATORY_CUSTOM_REG |
  510. REGULATORY_STRICT_REG |
  511. REGULATORY_COUNTRY_IE_FOLLOW_POWER |
  512. REGULATORY_COUNTRY_IE_IGNORE))))
  513. return -EINVAL;
  514. if (WARN_ON(wiphy->coalesce &&
  515. (!wiphy->coalesce->n_rules ||
  516. !wiphy->coalesce->n_patterns) &&
  517. (!wiphy->coalesce->pattern_min_len ||
  518. wiphy->coalesce->pattern_min_len >
  519. wiphy->coalesce->pattern_max_len)))
  520. return -EINVAL;
  521. if (WARN_ON(wiphy->ap_sme_capa &&
  522. !(wiphy->flags & WIPHY_FLAG_HAVE_AP_SME)))
  523. return -EINVAL;
  524. if (WARN_ON(wiphy->addresses && !wiphy->n_addresses))
  525. return -EINVAL;
  526. if (WARN_ON(wiphy->addresses &&
  527. !is_zero_ether_addr(wiphy->perm_addr) &&
  528. memcmp(wiphy->perm_addr, wiphy->addresses[0].addr,
  529. ETH_ALEN)))
  530. return -EINVAL;
  531. if (WARN_ON(wiphy->max_acl_mac_addrs &&
  532. (!(wiphy->flags & WIPHY_FLAG_HAVE_AP_SME) ||
  533. !rdev->ops->set_mac_acl)))
  534. return -EINVAL;
  535. /* assure only valid behaviours are flagged by driver
  536. * hence subtract 2 as bit 0 is invalid.
  537. */
  538. if (WARN_ON(wiphy->bss_select_support &&
  539. (wiphy->bss_select_support & ~(BIT(__NL80211_BSS_SELECT_ATTR_AFTER_LAST) - 2))))
  540. return -EINVAL;
  541. if (wiphy->addresses)
  542. memcpy(wiphy->perm_addr, wiphy->addresses[0].addr, ETH_ALEN);
  543. /* sanity check ifmodes */
  544. WARN_ON(!ifmodes);
  545. ifmodes &= ((1 << NUM_NL80211_IFTYPES) - 1) & ~1;
  546. if (WARN_ON(ifmodes != wiphy->interface_modes))
  547. wiphy->interface_modes = ifmodes;
  548. res = wiphy_verify_combinations(wiphy);
  549. if (res)
  550. return res;
  551. /* sanity check supported bands/channels */
  552. for (band = 0; band < NUM_NL80211_BANDS; band++) {
  553. sband = wiphy->bands[band];
  554. if (!sband)
  555. continue;
  556. sband->band = band;
  557. if (WARN_ON(!sband->n_channels))
  558. return -EINVAL;
  559. /*
  560. * on 60GHz band, there are no legacy rates, so
  561. * n_bitrates is 0
  562. */
  563. if (WARN_ON(band != NL80211_BAND_60GHZ &&
  564. !sband->n_bitrates))
  565. return -EINVAL;
  566. /*
  567. * Since cfg80211_disable_40mhz_24ghz is global, we can
  568. * modify the sband's ht data even if the driver uses a
  569. * global structure for that.
  570. */
  571. if (cfg80211_disable_40mhz_24ghz &&
  572. band == NL80211_BAND_2GHZ &&
  573. sband->ht_cap.ht_supported) {
  574. sband->ht_cap.cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  575. sband->ht_cap.cap &= ~IEEE80211_HT_CAP_SGI_40;
  576. }
  577. /*
  578. * Since we use a u32 for rate bitmaps in
  579. * ieee80211_get_response_rate, we cannot
  580. * have more than 32 legacy rates.
  581. */
  582. if (WARN_ON(sband->n_bitrates > 32))
  583. return -EINVAL;
  584. for (i = 0; i < sband->n_channels; i++) {
  585. sband->channels[i].orig_flags =
  586. sband->channels[i].flags;
  587. sband->channels[i].orig_mag = INT_MAX;
  588. sband->channels[i].orig_mpwr =
  589. sband->channels[i].max_power;
  590. sband->channels[i].band = band;
  591. }
  592. have_band = true;
  593. }
  594. if (!have_band) {
  595. WARN_ON(1);
  596. return -EINVAL;
  597. }
  598. #ifdef CONFIG_PM
  599. if (WARN_ON(rdev->wiphy.wowlan && rdev->wiphy.wowlan->n_patterns &&
  600. (!rdev->wiphy.wowlan->pattern_min_len ||
  601. rdev->wiphy.wowlan->pattern_min_len >
  602. rdev->wiphy.wowlan->pattern_max_len)))
  603. return -EINVAL;
  604. #endif
  605. /* check and set up bitrates */
  606. ieee80211_set_bitrate_flags(wiphy);
  607. rdev->wiphy.features |= NL80211_FEATURE_SCAN_FLUSH;
  608. rtnl_lock();
  609. res = device_add(&rdev->wiphy.dev);
  610. if (res) {
  611. rtnl_unlock();
  612. return res;
  613. }
  614. /* set up regulatory info */
  615. wiphy_regulatory_register(wiphy);
  616. list_add_rcu(&rdev->list, &cfg80211_rdev_list);
  617. cfg80211_rdev_list_generation++;
  618. /* add to debugfs */
  619. rdev->wiphy.debugfsdir =
  620. debugfs_create_dir(wiphy_name(&rdev->wiphy),
  621. ieee80211_debugfs_dir);
  622. if (IS_ERR(rdev->wiphy.debugfsdir))
  623. rdev->wiphy.debugfsdir = NULL;
  624. cfg80211_debugfs_rdev_add(rdev);
  625. nl80211_notify_wiphy(rdev, NL80211_CMD_NEW_WIPHY);
  626. if (wiphy->regulatory_flags & REGULATORY_CUSTOM_REG) {
  627. struct regulatory_request request;
  628. request.wiphy_idx = get_wiphy_idx(wiphy);
  629. request.initiator = NL80211_REGDOM_SET_BY_DRIVER;
  630. request.alpha2[0] = '9';
  631. request.alpha2[1] = '9';
  632. nl80211_send_reg_change_event(&request);
  633. }
  634. /* Check that nobody globally advertises any capabilities they do not
  635. * advertise on all possible interface types.
  636. */
  637. if (wiphy->extended_capabilities_len &&
  638. wiphy->num_iftype_ext_capab &&
  639. wiphy->iftype_ext_capab) {
  640. u8 supported_on_all, j;
  641. const struct wiphy_iftype_ext_capab *capab;
  642. capab = wiphy->iftype_ext_capab;
  643. for (j = 0; j < wiphy->extended_capabilities_len; j++) {
  644. if (capab[0].extended_capabilities_len > j)
  645. supported_on_all =
  646. capab[0].extended_capabilities[j];
  647. else
  648. supported_on_all = 0x00;
  649. for (i = 1; i < wiphy->num_iftype_ext_capab; i++) {
  650. if (j >= capab[i].extended_capabilities_len) {
  651. supported_on_all = 0x00;
  652. break;
  653. }
  654. supported_on_all &=
  655. capab[i].extended_capabilities[j];
  656. }
  657. if (WARN_ON(wiphy->extended_capabilities[j] &
  658. ~supported_on_all))
  659. break;
  660. }
  661. }
  662. rdev->wiphy.registered = true;
  663. rtnl_unlock();
  664. res = rfkill_register(rdev->rfkill);
  665. if (res) {
  666. rfkill_destroy(rdev->rfkill);
  667. rdev->rfkill = NULL;
  668. wiphy_unregister(&rdev->wiphy);
  669. return res;
  670. }
  671. return 0;
  672. }
  673. EXPORT_SYMBOL(wiphy_register);
  674. void wiphy_rfkill_start_polling(struct wiphy *wiphy)
  675. {
  676. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  677. if (!rdev->ops->rfkill_poll)
  678. return;
  679. rdev->rfkill_ops.poll = cfg80211_rfkill_poll;
  680. rfkill_resume_polling(rdev->rfkill);
  681. }
  682. EXPORT_SYMBOL(wiphy_rfkill_start_polling);
  683. void wiphy_rfkill_stop_polling(struct wiphy *wiphy)
  684. {
  685. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  686. rfkill_pause_polling(rdev->rfkill);
  687. }
  688. EXPORT_SYMBOL(wiphy_rfkill_stop_polling);
  689. void wiphy_unregister(struct wiphy *wiphy)
  690. {
  691. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  692. wait_event(rdev->dev_wait, ({
  693. int __count;
  694. rtnl_lock();
  695. __count = rdev->opencount;
  696. rtnl_unlock();
  697. __count == 0; }));
  698. if (rdev->rfkill)
  699. rfkill_unregister(rdev->rfkill);
  700. rtnl_lock();
  701. nl80211_notify_wiphy(rdev, NL80211_CMD_DEL_WIPHY);
  702. rdev->wiphy.registered = false;
  703. WARN_ON(!list_empty(&rdev->wiphy.wdev_list));
  704. /*
  705. * First remove the hardware from everywhere, this makes
  706. * it impossible to find from userspace.
  707. */
  708. debugfs_remove_recursive(rdev->wiphy.debugfsdir);
  709. list_del_rcu(&rdev->list);
  710. synchronize_rcu();
  711. /*
  712. * If this device got a regulatory hint tell core its
  713. * free to listen now to a new shiny device regulatory hint
  714. */
  715. wiphy_regulatory_deregister(wiphy);
  716. cfg80211_rdev_list_generation++;
  717. device_del(&rdev->wiphy.dev);
  718. rtnl_unlock();
  719. flush_work(&rdev->scan_done_wk);
  720. cancel_work_sync(&rdev->conn_work);
  721. flush_work(&rdev->event_work);
  722. cancel_delayed_work_sync(&rdev->dfs_update_channels_wk);
  723. flush_work(&rdev->destroy_work);
  724. flush_work(&rdev->sched_scan_stop_wk);
  725. flush_work(&rdev->mlme_unreg_wk);
  726. #ifdef CONFIG_PM
  727. if (rdev->wiphy.wowlan_config && rdev->ops->set_wakeup)
  728. rdev_set_wakeup(rdev, false);
  729. #endif
  730. cfg80211_rdev_free_wowlan(rdev);
  731. cfg80211_rdev_free_coalesce(rdev);
  732. }
  733. EXPORT_SYMBOL(wiphy_unregister);
  734. void cfg80211_dev_free(struct cfg80211_registered_device *rdev)
  735. {
  736. struct cfg80211_internal_bss *scan, *tmp;
  737. struct cfg80211_beacon_registration *reg, *treg;
  738. rfkill_destroy(rdev->rfkill);
  739. list_for_each_entry_safe(reg, treg, &rdev->beacon_registrations, list) {
  740. list_del(&reg->list);
  741. kfree(reg);
  742. }
  743. list_for_each_entry_safe(scan, tmp, &rdev->bss_list, list)
  744. cfg80211_put_bss(&rdev->wiphy, &scan->pub);
  745. kfree(rdev);
  746. }
  747. void wiphy_free(struct wiphy *wiphy)
  748. {
  749. put_device(&wiphy->dev);
  750. }
  751. EXPORT_SYMBOL(wiphy_free);
  752. void wiphy_rfkill_set_hw_state(struct wiphy *wiphy, bool blocked)
  753. {
  754. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  755. if (rfkill_set_hw_state(rdev->rfkill, blocked))
  756. schedule_work(&rdev->rfkill_sync);
  757. }
  758. EXPORT_SYMBOL(wiphy_rfkill_set_hw_state);
  759. void cfg80211_unregister_wdev(struct wireless_dev *wdev)
  760. {
  761. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  762. ASSERT_RTNL();
  763. if (WARN_ON(wdev->netdev))
  764. return;
  765. nl80211_notify_iface(rdev, wdev, NL80211_CMD_DEL_INTERFACE);
  766. list_del_rcu(&wdev->list);
  767. rdev->devlist_generation++;
  768. switch (wdev->iftype) {
  769. case NL80211_IFTYPE_P2P_DEVICE:
  770. cfg80211_mlme_purge_registrations(wdev);
  771. cfg80211_stop_p2p_device(rdev, wdev);
  772. break;
  773. case NL80211_IFTYPE_NAN:
  774. cfg80211_stop_nan(rdev, wdev);
  775. break;
  776. default:
  777. WARN_ON_ONCE(1);
  778. break;
  779. }
  780. }
  781. EXPORT_SYMBOL(cfg80211_unregister_wdev);
  782. static const struct device_type wiphy_type = {
  783. .name = "wlan",
  784. };
  785. void cfg80211_update_iface_num(struct cfg80211_registered_device *rdev,
  786. enum nl80211_iftype iftype, int num)
  787. {
  788. ASSERT_RTNL();
  789. rdev->num_running_ifaces += num;
  790. if (iftype == NL80211_IFTYPE_MONITOR)
  791. rdev->num_running_monitor_ifaces += num;
  792. }
  793. void __cfg80211_leave(struct cfg80211_registered_device *rdev,
  794. struct wireless_dev *wdev)
  795. {
  796. struct net_device *dev = wdev->netdev;
  797. struct cfg80211_sched_scan_request *sched_scan_req;
  798. ASSERT_RTNL();
  799. ASSERT_WDEV_LOCK(wdev);
  800. switch (wdev->iftype) {
  801. case NL80211_IFTYPE_ADHOC:
  802. __cfg80211_leave_ibss(rdev, dev, true);
  803. break;
  804. case NL80211_IFTYPE_P2P_CLIENT:
  805. case NL80211_IFTYPE_STATION:
  806. sched_scan_req = rtnl_dereference(rdev->sched_scan_req);
  807. if (sched_scan_req && dev == sched_scan_req->dev)
  808. __cfg80211_stop_sched_scan(rdev, false);
  809. #ifdef CONFIG_CFG80211_WEXT
  810. kfree(wdev->wext.ie);
  811. wdev->wext.ie = NULL;
  812. wdev->wext.ie_len = 0;
  813. wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
  814. #endif
  815. cfg80211_disconnect(rdev, dev,
  816. WLAN_REASON_DEAUTH_LEAVING, true);
  817. break;
  818. case NL80211_IFTYPE_MESH_POINT:
  819. __cfg80211_leave_mesh(rdev, dev);
  820. break;
  821. case NL80211_IFTYPE_AP:
  822. case NL80211_IFTYPE_P2P_GO:
  823. __cfg80211_stop_ap(rdev, dev, true);
  824. break;
  825. case NL80211_IFTYPE_OCB:
  826. __cfg80211_leave_ocb(rdev, dev);
  827. break;
  828. case NL80211_IFTYPE_WDS:
  829. /* must be handled by mac80211/driver, has no APIs */
  830. break;
  831. case NL80211_IFTYPE_P2P_DEVICE:
  832. case NL80211_IFTYPE_NAN:
  833. /* cannot happen, has no netdev */
  834. break;
  835. case NL80211_IFTYPE_AP_VLAN:
  836. case NL80211_IFTYPE_MONITOR:
  837. /* nothing to do */
  838. break;
  839. case NL80211_IFTYPE_UNSPECIFIED:
  840. case NUM_NL80211_IFTYPES:
  841. /* invalid */
  842. break;
  843. }
  844. }
  845. void cfg80211_leave(struct cfg80211_registered_device *rdev,
  846. struct wireless_dev *wdev)
  847. {
  848. wdev_lock(wdev);
  849. __cfg80211_leave(rdev, wdev);
  850. wdev_unlock(wdev);
  851. }
  852. void cfg80211_stop_iface(struct wiphy *wiphy, struct wireless_dev *wdev,
  853. gfp_t gfp)
  854. {
  855. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  856. struct cfg80211_event *ev;
  857. unsigned long flags;
  858. trace_cfg80211_stop_iface(wiphy, wdev);
  859. ev = kzalloc(sizeof(*ev), gfp);
  860. if (!ev)
  861. return;
  862. ev->type = EVENT_STOPPED;
  863. spin_lock_irqsave(&wdev->event_lock, flags);
  864. list_add_tail(&ev->list, &wdev->event_list);
  865. spin_unlock_irqrestore(&wdev->event_lock, flags);
  866. queue_work(cfg80211_wq, &rdev->event_work);
  867. }
  868. EXPORT_SYMBOL(cfg80211_stop_iface);
  869. static int cfg80211_netdev_notifier_call(struct notifier_block *nb,
  870. unsigned long state, void *ptr)
  871. {
  872. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  873. struct wireless_dev *wdev = dev->ieee80211_ptr;
  874. struct cfg80211_registered_device *rdev;
  875. struct cfg80211_sched_scan_request *sched_scan_req;
  876. if (!wdev)
  877. return NOTIFY_DONE;
  878. rdev = wiphy_to_rdev(wdev->wiphy);
  879. WARN_ON(wdev->iftype == NL80211_IFTYPE_UNSPECIFIED);
  880. switch (state) {
  881. case NETDEV_POST_INIT:
  882. SET_NETDEV_DEVTYPE(dev, &wiphy_type);
  883. break;
  884. case NETDEV_REGISTER:
  885. /*
  886. * NB: cannot take rdev->mtx here because this may be
  887. * called within code protected by it when interfaces
  888. * are added with nl80211.
  889. */
  890. mutex_init(&wdev->mtx);
  891. INIT_LIST_HEAD(&wdev->event_list);
  892. spin_lock_init(&wdev->event_lock);
  893. INIT_LIST_HEAD(&wdev->mgmt_registrations);
  894. spin_lock_init(&wdev->mgmt_registrations_lock);
  895. wdev->identifier = ++rdev->wdev_id;
  896. list_add_rcu(&wdev->list, &rdev->wiphy.wdev_list);
  897. rdev->devlist_generation++;
  898. /* can only change netns with wiphy */
  899. dev->features |= NETIF_F_NETNS_LOCAL;
  900. if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj,
  901. "phy80211")) {
  902. pr_err("failed to add phy80211 symlink to netdev!\n");
  903. }
  904. wdev->netdev = dev;
  905. #ifdef CONFIG_CFG80211_WEXT
  906. wdev->wext.default_key = -1;
  907. wdev->wext.default_mgmt_key = -1;
  908. wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
  909. #endif
  910. if (wdev->wiphy->flags & WIPHY_FLAG_PS_ON_BY_DEFAULT)
  911. wdev->ps = true;
  912. else
  913. wdev->ps = false;
  914. /* allow mac80211 to determine the timeout */
  915. wdev->ps_timeout = -1;
  916. if ((wdev->iftype == NL80211_IFTYPE_STATION ||
  917. wdev->iftype == NL80211_IFTYPE_P2P_CLIENT ||
  918. wdev->iftype == NL80211_IFTYPE_ADHOC) && !wdev->use_4addr)
  919. dev->priv_flags |= IFF_DONT_BRIDGE;
  920. INIT_WORK(&wdev->disconnect_wk, cfg80211_autodisconnect_wk);
  921. nl80211_notify_iface(rdev, wdev, NL80211_CMD_NEW_INTERFACE);
  922. break;
  923. case NETDEV_GOING_DOWN:
  924. cfg80211_leave(rdev, wdev);
  925. break;
  926. case NETDEV_DOWN:
  927. cfg80211_update_iface_num(rdev, wdev->iftype, -1);
  928. if (rdev->scan_req && rdev->scan_req->wdev == wdev) {
  929. if (WARN_ON(!rdev->scan_req->notified))
  930. rdev->scan_req->info.aborted = true;
  931. ___cfg80211_scan_done(rdev, false);
  932. }
  933. sched_scan_req = rtnl_dereference(rdev->sched_scan_req);
  934. if (WARN_ON(sched_scan_req &&
  935. sched_scan_req->dev == wdev->netdev)) {
  936. __cfg80211_stop_sched_scan(rdev, false);
  937. }
  938. rdev->opencount--;
  939. wake_up(&rdev->dev_wait);
  940. break;
  941. case NETDEV_UP:
  942. cfg80211_update_iface_num(rdev, wdev->iftype, 1);
  943. wdev_lock(wdev);
  944. switch (wdev->iftype) {
  945. #ifdef CONFIG_CFG80211_WEXT
  946. case NL80211_IFTYPE_ADHOC:
  947. cfg80211_ibss_wext_join(rdev, wdev);
  948. break;
  949. case NL80211_IFTYPE_STATION:
  950. cfg80211_mgd_wext_connect(rdev, wdev);
  951. break;
  952. #endif
  953. #ifdef CONFIG_MAC80211_MESH
  954. case NL80211_IFTYPE_MESH_POINT:
  955. {
  956. /* backward compat code... */
  957. struct mesh_setup setup;
  958. memcpy(&setup, &default_mesh_setup,
  959. sizeof(setup));
  960. /* back compat only needed for mesh_id */
  961. setup.mesh_id = wdev->ssid;
  962. setup.mesh_id_len = wdev->mesh_id_up_len;
  963. if (wdev->mesh_id_up_len)
  964. __cfg80211_join_mesh(rdev, dev,
  965. &setup,
  966. &default_mesh_config);
  967. break;
  968. }
  969. #endif
  970. default:
  971. break;
  972. }
  973. wdev_unlock(wdev);
  974. rdev->opencount++;
  975. /*
  976. * Configure power management to the driver here so that its
  977. * correctly set also after interface type changes etc.
  978. */
  979. if ((wdev->iftype == NL80211_IFTYPE_STATION ||
  980. wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) &&
  981. rdev->ops->set_power_mgmt)
  982. if (rdev_set_power_mgmt(rdev, dev, wdev->ps,
  983. wdev->ps_timeout)) {
  984. /* assume this means it's off */
  985. wdev->ps = false;
  986. }
  987. break;
  988. case NETDEV_UNREGISTER:
  989. /*
  990. * It is possible to get NETDEV_UNREGISTER
  991. * multiple times. To detect that, check
  992. * that the interface is still on the list
  993. * of registered interfaces, and only then
  994. * remove and clean it up.
  995. */
  996. if (!list_empty(&wdev->list)) {
  997. nl80211_notify_iface(rdev, wdev,
  998. NL80211_CMD_DEL_INTERFACE);
  999. sysfs_remove_link(&dev->dev.kobj, "phy80211");
  1000. list_del_rcu(&wdev->list);
  1001. rdev->devlist_generation++;
  1002. cfg80211_mlme_purge_registrations(wdev);
  1003. #ifdef CONFIG_CFG80211_WEXT
  1004. kzfree(wdev->wext.keys);
  1005. #endif
  1006. flush_work(&wdev->disconnect_wk);
  1007. }
  1008. /*
  1009. * synchronise (so that we won't find this netdev
  1010. * from other code any more) and then clear the list
  1011. * head so that the above code can safely check for
  1012. * !list_empty() to avoid double-cleanup.
  1013. */
  1014. synchronize_rcu();
  1015. INIT_LIST_HEAD(&wdev->list);
  1016. /*
  1017. * Ensure that all events have been processed and
  1018. * freed.
  1019. */
  1020. cfg80211_process_wdev_events(wdev);
  1021. if (WARN_ON(wdev->current_bss)) {
  1022. cfg80211_unhold_bss(wdev->current_bss);
  1023. cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
  1024. wdev->current_bss = NULL;
  1025. }
  1026. break;
  1027. case NETDEV_PRE_UP:
  1028. if (!(wdev->wiphy->interface_modes & BIT(wdev->iftype)))
  1029. return notifier_from_errno(-EOPNOTSUPP);
  1030. if (rfkill_blocked(rdev->rfkill))
  1031. return notifier_from_errno(-ERFKILL);
  1032. break;
  1033. default:
  1034. return NOTIFY_DONE;
  1035. }
  1036. wireless_nlevent_flush();
  1037. return NOTIFY_OK;
  1038. }
  1039. static struct notifier_block cfg80211_netdev_notifier = {
  1040. .notifier_call = cfg80211_netdev_notifier_call,
  1041. };
  1042. static void __net_exit cfg80211_pernet_exit(struct net *net)
  1043. {
  1044. struct cfg80211_registered_device *rdev;
  1045. rtnl_lock();
  1046. list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
  1047. if (net_eq(wiphy_net(&rdev->wiphy), net))
  1048. WARN_ON(cfg80211_switch_netns(rdev, &init_net));
  1049. }
  1050. rtnl_unlock();
  1051. }
  1052. static struct pernet_operations cfg80211_pernet_ops = {
  1053. .exit = cfg80211_pernet_exit,
  1054. };
  1055. static int __init cfg80211_init(void)
  1056. {
  1057. int err;
  1058. err = register_pernet_device(&cfg80211_pernet_ops);
  1059. if (err)
  1060. goto out_fail_pernet;
  1061. err = wiphy_sysfs_init();
  1062. if (err)
  1063. goto out_fail_sysfs;
  1064. err = register_netdevice_notifier(&cfg80211_netdev_notifier);
  1065. if (err)
  1066. goto out_fail_notifier;
  1067. err = nl80211_init();
  1068. if (err)
  1069. goto out_fail_nl80211;
  1070. ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
  1071. err = regulatory_init();
  1072. if (err)
  1073. goto out_fail_reg;
  1074. cfg80211_wq = alloc_ordered_workqueue("cfg80211", WQ_MEM_RECLAIM);
  1075. if (!cfg80211_wq) {
  1076. err = -ENOMEM;
  1077. goto out_fail_wq;
  1078. }
  1079. return 0;
  1080. out_fail_wq:
  1081. regulatory_exit();
  1082. out_fail_reg:
  1083. debugfs_remove(ieee80211_debugfs_dir);
  1084. nl80211_exit();
  1085. out_fail_nl80211:
  1086. unregister_netdevice_notifier(&cfg80211_netdev_notifier);
  1087. out_fail_notifier:
  1088. wiphy_sysfs_exit();
  1089. out_fail_sysfs:
  1090. unregister_pernet_device(&cfg80211_pernet_ops);
  1091. out_fail_pernet:
  1092. return err;
  1093. }
  1094. subsys_initcall(cfg80211_init);
  1095. static void __exit cfg80211_exit(void)
  1096. {
  1097. debugfs_remove(ieee80211_debugfs_dir);
  1098. nl80211_exit();
  1099. unregister_netdevice_notifier(&cfg80211_netdev_notifier);
  1100. wiphy_sysfs_exit();
  1101. regulatory_exit();
  1102. unregister_pernet_device(&cfg80211_pernet_ops);
  1103. destroy_workqueue(cfg80211_wq);
  1104. }
  1105. module_exit(cfg80211_exit);