core.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * This is the linux wireless configuration interface.
  4. *
  5. * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
  6. * Copyright 2013-2014 Intel Mobile Communications GmbH
  7. * Copyright 2015-2017 Intel Deutschland GmbH
  8. * Copyright (C) 2018-2025 Intel Corporation
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/if.h>
  12. #include <linux/module.h>
  13. #include <linux/err.h>
  14. #include <linux/list.h>
  15. #include <linux/slab.h>
  16. #include <linux/nl80211.h>
  17. #include <linux/debugfs.h>
  18. #include <linux/notifier.h>
  19. #include <linux/device.h>
  20. #include <linux/etherdevice.h>
  21. #include <linux/rtnetlink.h>
  22. #include <linux/sched.h>
  23. #include <net/genetlink.h>
  24. #include <net/cfg80211.h>
  25. #include "nl80211.h"
  26. #include "core.h"
  27. #include "sysfs.h"
  28. #include "debugfs.h"
  29. #include "wext-compat.h"
  30. #include "rdev-ops.h"
  31. /* name for sysfs, %d is appended */
  32. #define PHY_NAME "phy"
  33. /* maximum length of radio debugfs directory name */
  34. #define RADIO_DEBUGFSDIR_MAX_LEN 8
  35. MODULE_AUTHOR("Johannes Berg");
  36. MODULE_LICENSE("GPL");
  37. MODULE_DESCRIPTION("wireless configuration support");
  38. MODULE_ALIAS_GENL_FAMILY(NL80211_GENL_NAME);
  39. /* RCU-protected (and RTNL for writers) */
  40. LIST_HEAD(cfg80211_rdev_list);
  41. int cfg80211_rdev_list_generation;
  42. /* for debugfs */
  43. static struct dentry *ieee80211_debugfs_dir;
  44. /* for the cleanup, scan and event works */
  45. struct workqueue_struct *cfg80211_wq;
  46. static bool cfg80211_disable_40mhz_24ghz;
  47. module_param(cfg80211_disable_40mhz_24ghz, bool, 0644);
  48. MODULE_PARM_DESC(cfg80211_disable_40mhz_24ghz,
  49. "Disable 40MHz support in the 2.4GHz band");
  50. struct cfg80211_registered_device *cfg80211_rdev_by_wiphy_idx(int wiphy_idx)
  51. {
  52. struct cfg80211_registered_device *result = NULL, *rdev;
  53. ASSERT_RTNL();
  54. for_each_rdev(rdev) {
  55. if (rdev->wiphy_idx == wiphy_idx) {
  56. result = rdev;
  57. break;
  58. }
  59. }
  60. return result;
  61. }
  62. int get_wiphy_idx(struct wiphy *wiphy)
  63. {
  64. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  65. return rdev->wiphy_idx;
  66. }
  67. struct wiphy *wiphy_idx_to_wiphy(int wiphy_idx)
  68. {
  69. struct cfg80211_registered_device *rdev;
  70. ASSERT_RTNL();
  71. rdev = cfg80211_rdev_by_wiphy_idx(wiphy_idx);
  72. if (!rdev)
  73. return NULL;
  74. return &rdev->wiphy;
  75. }
  76. static int cfg80211_dev_check_name(struct cfg80211_registered_device *rdev,
  77. const char *newname)
  78. {
  79. struct cfg80211_registered_device *rdev2;
  80. int wiphy_idx, taken = -1, digits;
  81. ASSERT_RTNL();
  82. if (strlen(newname) > NL80211_WIPHY_NAME_MAXLEN)
  83. return -EINVAL;
  84. /* prohibit calling the thing phy%d when %d is not its number */
  85. sscanf(newname, PHY_NAME "%d%n", &wiphy_idx, &taken);
  86. if (taken == strlen(newname) && wiphy_idx != rdev->wiphy_idx) {
  87. /* count number of places needed to print wiphy_idx */
  88. digits = 1;
  89. while (wiphy_idx /= 10)
  90. digits++;
  91. /*
  92. * deny the name if it is phy<idx> where <idx> is printed
  93. * without leading zeroes. taken == strlen(newname) here
  94. */
  95. if (taken == strlen(PHY_NAME) + digits)
  96. return -EINVAL;
  97. }
  98. /* Ensure another device does not already have this name. */
  99. for_each_rdev(rdev2)
  100. if (strcmp(newname, wiphy_name(&rdev2->wiphy)) == 0)
  101. return -EINVAL;
  102. return 0;
  103. }
  104. int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
  105. char *newname)
  106. {
  107. int result;
  108. ASSERT_RTNL();
  109. lockdep_assert_wiphy(&rdev->wiphy);
  110. /* Ignore nop renames */
  111. if (strcmp(newname, wiphy_name(&rdev->wiphy)) == 0)
  112. return 0;
  113. result = cfg80211_dev_check_name(rdev, newname);
  114. if (result < 0)
  115. return result;
  116. result = device_rename(&rdev->wiphy.dev, newname);
  117. if (result)
  118. return result;
  119. debugfs_change_name(rdev->wiphy.debugfsdir, "%s", 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->netns_immutable = false;
  134. err = dev_change_net_namespace(wdev->netdev, net, "wlan%d");
  135. if (err)
  136. break;
  137. wdev->netdev->netns_immutable = true;
  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->netns_immutable = false;
  148. err = dev_change_net_namespace(wdev->netdev, net,
  149. "wlan%d");
  150. WARN_ON(err);
  151. wdev->netdev->netns_immutable = true;
  152. }
  153. return err;
  154. }
  155. guard(wiphy)(&rdev->wiphy);
  156. list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
  157. if (!wdev->netdev)
  158. continue;
  159. nl80211_notify_iface(rdev, wdev, NL80211_CMD_DEL_INTERFACE);
  160. }
  161. nl80211_notify_wiphy(rdev, NL80211_CMD_DEL_WIPHY);
  162. wiphy_net_set(&rdev->wiphy, net);
  163. err = device_rename(&rdev->wiphy.dev, dev_name(&rdev->wiphy.dev));
  164. WARN_ON(err);
  165. nl80211_notify_wiphy(rdev, NL80211_CMD_NEW_WIPHY);
  166. list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
  167. if (!wdev->netdev)
  168. continue;
  169. nl80211_notify_iface(rdev, wdev, NL80211_CMD_NEW_INTERFACE);
  170. }
  171. return 0;
  172. }
  173. static void cfg80211_rfkill_poll(struct rfkill *rfkill, void *data)
  174. {
  175. struct cfg80211_registered_device *rdev = data;
  176. guard(wiphy)(&rdev->wiphy);
  177. rdev_rfkill_poll(rdev);
  178. }
  179. void cfg80211_stop_p2p_device(struct cfg80211_registered_device *rdev,
  180. struct wireless_dev *wdev)
  181. {
  182. lockdep_assert_held(&rdev->wiphy.mtx);
  183. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_P2P_DEVICE))
  184. return;
  185. if (!wdev_running(wdev))
  186. return;
  187. rdev_stop_p2p_device(rdev, wdev);
  188. wdev->is_running = false;
  189. rdev->opencount--;
  190. if (rdev->scan_req && rdev->scan_req->req.wdev == wdev) {
  191. if (WARN_ON(!rdev->scan_req->notified &&
  192. (!rdev->int_scan_req ||
  193. !rdev->int_scan_req->notified)))
  194. rdev->scan_req->info.aborted = true;
  195. ___cfg80211_scan_done(rdev, false);
  196. }
  197. }
  198. void cfg80211_stop_nan(struct cfg80211_registered_device *rdev,
  199. struct wireless_dev *wdev)
  200. {
  201. lockdep_assert_held(&rdev->wiphy.mtx);
  202. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_NAN))
  203. return;
  204. if (!wdev_running(wdev))
  205. return;
  206. rdev_stop_nan(rdev, wdev);
  207. wdev->is_running = false;
  208. eth_zero_addr(wdev->u.nan.cluster_id);
  209. rdev->opencount--;
  210. }
  211. void cfg80211_shutdown_all_interfaces(struct wiphy *wiphy)
  212. {
  213. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  214. struct wireless_dev *wdev;
  215. ASSERT_RTNL();
  216. list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
  217. if (wdev->netdev) {
  218. dev_close(wdev->netdev);
  219. continue;
  220. }
  221. /* otherwise, check iftype */
  222. guard(wiphy)(wiphy);
  223. switch (wdev->iftype) {
  224. case NL80211_IFTYPE_P2P_DEVICE:
  225. cfg80211_stop_p2p_device(rdev, wdev);
  226. break;
  227. case NL80211_IFTYPE_NAN:
  228. cfg80211_stop_nan(rdev, wdev);
  229. break;
  230. default:
  231. break;
  232. }
  233. }
  234. }
  235. EXPORT_SYMBOL_GPL(cfg80211_shutdown_all_interfaces);
  236. static int cfg80211_rfkill_set_block(void *data, bool blocked)
  237. {
  238. struct cfg80211_registered_device *rdev = data;
  239. if (!blocked)
  240. return 0;
  241. rtnl_lock();
  242. cfg80211_shutdown_all_interfaces(&rdev->wiphy);
  243. rtnl_unlock();
  244. return 0;
  245. }
  246. static void cfg80211_rfkill_block_work(struct work_struct *work)
  247. {
  248. struct cfg80211_registered_device *rdev;
  249. rdev = container_of(work, struct cfg80211_registered_device,
  250. rfkill_block);
  251. cfg80211_rfkill_set_block(rdev, true);
  252. }
  253. static void cfg80211_event_work(struct work_struct *work)
  254. {
  255. struct cfg80211_registered_device *rdev;
  256. rdev = container_of(work, struct cfg80211_registered_device,
  257. event_work);
  258. guard(wiphy)(&rdev->wiphy);
  259. cfg80211_process_rdev_events(rdev);
  260. }
  261. void cfg80211_destroy_ifaces(struct cfg80211_registered_device *rdev)
  262. {
  263. struct wireless_dev *wdev, *tmp;
  264. ASSERT_RTNL();
  265. list_for_each_entry_safe(wdev, tmp, &rdev->wiphy.wdev_list, list) {
  266. if (wdev->nl_owner_dead) {
  267. if (wdev->netdev)
  268. dev_close(wdev->netdev);
  269. guard(wiphy)(&rdev->wiphy);
  270. cfg80211_leave(rdev, wdev, -1);
  271. cfg80211_remove_virtual_intf(rdev, wdev);
  272. }
  273. }
  274. }
  275. static void cfg80211_destroy_iface_wk(struct work_struct *work)
  276. {
  277. struct cfg80211_registered_device *rdev;
  278. rdev = container_of(work, struct cfg80211_registered_device,
  279. destroy_work);
  280. rtnl_lock();
  281. cfg80211_destroy_ifaces(rdev);
  282. rtnl_unlock();
  283. }
  284. static void cfg80211_sched_scan_stop_wk(struct wiphy *wiphy,
  285. struct wiphy_work *work)
  286. {
  287. struct cfg80211_registered_device *rdev;
  288. struct cfg80211_sched_scan_request *req, *tmp;
  289. rdev = container_of(work, struct cfg80211_registered_device,
  290. sched_scan_stop_wk);
  291. list_for_each_entry_safe(req, tmp, &rdev->sched_scan_req_list, list) {
  292. if (req->nl_owner_dead)
  293. cfg80211_stop_sched_scan_req(rdev, req, false);
  294. }
  295. }
  296. static void cfg80211_propagate_radar_detect_wk(struct work_struct *work)
  297. {
  298. struct cfg80211_registered_device *rdev;
  299. rdev = container_of(work, struct cfg80211_registered_device,
  300. propagate_radar_detect_wk);
  301. rtnl_lock();
  302. regulatory_propagate_dfs_state(&rdev->wiphy, &rdev->radar_chandef,
  303. NL80211_DFS_UNAVAILABLE,
  304. NL80211_RADAR_DETECTED);
  305. rtnl_unlock();
  306. }
  307. static void cfg80211_propagate_cac_done_wk(struct work_struct *work)
  308. {
  309. struct cfg80211_registered_device *rdev;
  310. rdev = container_of(work, struct cfg80211_registered_device,
  311. propagate_cac_done_wk);
  312. rtnl_lock();
  313. regulatory_propagate_dfs_state(&rdev->wiphy, &rdev->cac_done_chandef,
  314. NL80211_DFS_AVAILABLE,
  315. NL80211_RADAR_CAC_FINISHED);
  316. rtnl_unlock();
  317. }
  318. static void cfg80211_wiphy_work(struct work_struct *work)
  319. {
  320. struct cfg80211_registered_device *rdev;
  321. struct wiphy_work *wk;
  322. rdev = container_of(work, struct cfg80211_registered_device, wiphy_work);
  323. trace_wiphy_work_worker_start(&rdev->wiphy);
  324. guard(wiphy)(&rdev->wiphy);
  325. if (rdev->suspended)
  326. return;
  327. spin_lock_irq(&rdev->wiphy_work_lock);
  328. wk = list_first_entry_or_null(&rdev->wiphy_work_list,
  329. struct wiphy_work, entry);
  330. if (wk) {
  331. list_del_init(&wk->entry);
  332. if (!list_empty(&rdev->wiphy_work_list))
  333. queue_work(system_dfl_wq, work);
  334. spin_unlock_irq(&rdev->wiphy_work_lock);
  335. trace_wiphy_work_run(&rdev->wiphy, wk);
  336. wk->func(&rdev->wiphy, wk);
  337. } else {
  338. spin_unlock_irq(&rdev->wiphy_work_lock);
  339. }
  340. }
  341. /* exported functions */
  342. struct wiphy *wiphy_new_nm(const struct cfg80211_ops *ops, int sizeof_priv,
  343. const char *requested_name)
  344. {
  345. static atomic_t wiphy_counter = ATOMIC_INIT(0);
  346. struct cfg80211_registered_device *rdev;
  347. int alloc_size;
  348. WARN_ON(ops->add_key && (!ops->del_key || !ops->set_default_key));
  349. WARN_ON(ops->auth && (!ops->assoc || !ops->deauth || !ops->disassoc));
  350. WARN_ON(ops->connect && !ops->disconnect);
  351. WARN_ON(ops->join_ibss && !ops->leave_ibss);
  352. WARN_ON(ops->add_virtual_intf && !ops->del_virtual_intf);
  353. WARN_ON(ops->add_station && !ops->del_station);
  354. WARN_ON(ops->add_mpath && !ops->del_mpath);
  355. WARN_ON(ops->join_mesh && !ops->leave_mesh);
  356. WARN_ON(ops->start_p2p_device && !ops->stop_p2p_device);
  357. WARN_ON(ops->start_ap && !ops->stop_ap);
  358. WARN_ON(ops->join_ocb && !ops->leave_ocb);
  359. WARN_ON(ops->suspend && !ops->resume);
  360. WARN_ON(ops->sched_scan_start && !ops->sched_scan_stop);
  361. WARN_ON(ops->remain_on_channel && !ops->cancel_remain_on_channel);
  362. WARN_ON(ops->tdls_channel_switch && !ops->tdls_cancel_channel_switch);
  363. WARN_ON(ops->add_tx_ts && !ops->del_tx_ts);
  364. alloc_size = sizeof(*rdev) + sizeof_priv;
  365. rdev = kzalloc(alloc_size, GFP_KERNEL);
  366. if (!rdev)
  367. return NULL;
  368. rdev->ops = ops;
  369. rdev->wiphy_idx = atomic_inc_return(&wiphy_counter);
  370. if (unlikely(rdev->wiphy_idx < 0)) {
  371. /* ugh, wrapped! */
  372. atomic_dec(&wiphy_counter);
  373. kfree(rdev);
  374. return NULL;
  375. }
  376. /* atomic_inc_return makes it start at 1, make it start at 0 */
  377. rdev->wiphy_idx--;
  378. /* give it a proper name */
  379. if (requested_name && requested_name[0]) {
  380. int rv;
  381. rtnl_lock();
  382. rv = cfg80211_dev_check_name(rdev, requested_name);
  383. if (rv < 0) {
  384. rtnl_unlock();
  385. goto use_default_name;
  386. }
  387. rv = dev_set_name(&rdev->wiphy.dev, "%s", requested_name);
  388. rtnl_unlock();
  389. if (rv)
  390. goto use_default_name;
  391. } else {
  392. int rv;
  393. use_default_name:
  394. /* NOTE: This is *probably* safe w/out holding rtnl because of
  395. * the restrictions on phy names. Probably this call could
  396. * fail if some other part of the kernel (re)named a device
  397. * phyX. But, might should add some locking and check return
  398. * value, and use a different name if this one exists?
  399. */
  400. rv = dev_set_name(&rdev->wiphy.dev, PHY_NAME "%d", rdev->wiphy_idx);
  401. if (rv < 0) {
  402. kfree(rdev);
  403. return NULL;
  404. }
  405. }
  406. mutex_init(&rdev->wiphy.mtx);
  407. INIT_LIST_HEAD(&rdev->wiphy.wdev_list);
  408. INIT_LIST_HEAD(&rdev->beacon_registrations);
  409. spin_lock_init(&rdev->beacon_registrations_lock);
  410. spin_lock_init(&rdev->bss_lock);
  411. INIT_LIST_HEAD(&rdev->bss_list);
  412. INIT_LIST_HEAD(&rdev->sched_scan_req_list);
  413. wiphy_work_init(&rdev->scan_done_wk, __cfg80211_scan_done);
  414. INIT_DELAYED_WORK(&rdev->dfs_update_channels_wk,
  415. cfg80211_dfs_channels_update_work);
  416. #ifdef CONFIG_CFG80211_WEXT
  417. rdev->wiphy.wext = &cfg80211_wext_handler;
  418. #endif
  419. device_initialize(&rdev->wiphy.dev);
  420. rdev->wiphy.dev.class = &ieee80211_class;
  421. rdev->wiphy.dev.platform_data = rdev;
  422. device_enable_async_suspend(&rdev->wiphy.dev);
  423. INIT_WORK(&rdev->destroy_work, cfg80211_destroy_iface_wk);
  424. wiphy_work_init(&rdev->sched_scan_stop_wk, cfg80211_sched_scan_stop_wk);
  425. INIT_WORK(&rdev->sched_scan_res_wk, cfg80211_sched_scan_results_wk);
  426. INIT_WORK(&rdev->propagate_radar_detect_wk,
  427. cfg80211_propagate_radar_detect_wk);
  428. INIT_WORK(&rdev->propagate_cac_done_wk, cfg80211_propagate_cac_done_wk);
  429. INIT_WORK(&rdev->mgmt_registrations_update_wk,
  430. cfg80211_mgmt_registrations_update_wk);
  431. spin_lock_init(&rdev->mgmt_registrations_lock);
  432. INIT_WORK(&rdev->wiphy_work, cfg80211_wiphy_work);
  433. INIT_LIST_HEAD(&rdev->wiphy_work_list);
  434. spin_lock_init(&rdev->wiphy_work_lock);
  435. #ifdef CONFIG_CFG80211_DEFAULT_PS
  436. rdev->wiphy.flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
  437. #endif
  438. wiphy_net_set(&rdev->wiphy, &init_net);
  439. rdev->rfkill_ops.set_block = cfg80211_rfkill_set_block;
  440. rdev->wiphy.rfkill = rfkill_alloc(dev_name(&rdev->wiphy.dev),
  441. &rdev->wiphy.dev, RFKILL_TYPE_WLAN,
  442. &rdev->rfkill_ops, rdev);
  443. if (!rdev->wiphy.rfkill) {
  444. wiphy_free(&rdev->wiphy);
  445. return NULL;
  446. }
  447. INIT_WORK(&rdev->rfkill_block, cfg80211_rfkill_block_work);
  448. INIT_WORK(&rdev->conn_work, cfg80211_conn_work);
  449. INIT_WORK(&rdev->event_work, cfg80211_event_work);
  450. INIT_WORK(&rdev->background_cac_abort_wk,
  451. cfg80211_background_cac_abort_wk);
  452. INIT_DELAYED_WORK(&rdev->background_cac_done_wk,
  453. cfg80211_background_cac_done_wk);
  454. init_waitqueue_head(&rdev->dev_wait);
  455. /*
  456. * Initialize wiphy parameters to IEEE 802.11 MIB default values.
  457. * Fragmentation and RTS threshold are disabled by default with the
  458. * special -1 value.
  459. */
  460. rdev->wiphy.retry_short = 7;
  461. rdev->wiphy.retry_long = 4;
  462. rdev->wiphy.frag_threshold = (u32) -1;
  463. rdev->wiphy.rts_threshold = (u32) -1;
  464. rdev->wiphy.coverage_class = 0;
  465. rdev->wiphy.max_num_csa_counters = 1;
  466. rdev->wiphy.max_sched_scan_plans = 1;
  467. rdev->wiphy.max_sched_scan_plan_interval = U32_MAX;
  468. return &rdev->wiphy;
  469. }
  470. EXPORT_SYMBOL(wiphy_new_nm);
  471. static
  472. int wiphy_verify_iface_combinations(struct wiphy *wiphy,
  473. const struct ieee80211_iface_combination *iface_comb,
  474. int n_iface_comb,
  475. bool combined_radio)
  476. {
  477. const struct ieee80211_iface_combination *c;
  478. int i, j;
  479. for (i = 0; i < n_iface_comb; i++) {
  480. u32 cnt = 0;
  481. u16 all_iftypes = 0;
  482. c = &iface_comb[i];
  483. /*
  484. * Combinations with just one interface aren't real,
  485. * however we make an exception for DFS.
  486. */
  487. if (WARN_ON((c->max_interfaces < 2) && !c->radar_detect_widths))
  488. return -EINVAL;
  489. /* Need at least one channel */
  490. if (WARN_ON(!c->num_different_channels))
  491. return -EINVAL;
  492. /* DFS only works on one channel. Avoid this check
  493. * for multi-radio global combination, since it hold
  494. * the capabilities of all radio combinations.
  495. */
  496. if (!combined_radio &&
  497. WARN_ON(c->radar_detect_widths &&
  498. c->num_different_channels > 1))
  499. return -EINVAL;
  500. if (WARN_ON(!c->n_limits))
  501. return -EINVAL;
  502. for (j = 0; j < c->n_limits; j++) {
  503. u16 types = c->limits[j].types;
  504. /* interface types shouldn't overlap */
  505. if (WARN_ON(types & all_iftypes))
  506. return -EINVAL;
  507. all_iftypes |= types;
  508. if (WARN_ON(!c->limits[j].max))
  509. return -EINVAL;
  510. /* Shouldn't list software iftypes in combinations! */
  511. if (WARN_ON(wiphy->software_iftypes & types))
  512. return -EINVAL;
  513. /* Only a single P2P_DEVICE can be allowed, avoid this
  514. * check for multi-radio global combination, since it
  515. * hold the capabilities of all radio combinations.
  516. */
  517. if (!combined_radio &&
  518. WARN_ON(types & BIT(NL80211_IFTYPE_P2P_DEVICE) &&
  519. c->limits[j].max > 1))
  520. return -EINVAL;
  521. /* Only a single NAN can be allowed */
  522. if (WARN_ON(types & BIT(NL80211_IFTYPE_NAN) &&
  523. c->limits[j].max > 1))
  524. return -EINVAL;
  525. /*
  526. * This isn't well-defined right now. If you have an
  527. * IBSS interface, then its beacon interval may change
  528. * by joining other networks, and nothing prevents it
  529. * from doing that.
  530. * So technically we probably shouldn't even allow AP
  531. * and IBSS in the same interface, but it seems that
  532. * some drivers support that, possibly only with fixed
  533. * beacon intervals for IBSS.
  534. */
  535. if (WARN_ON(types & BIT(NL80211_IFTYPE_ADHOC) &&
  536. c->beacon_int_min_gcd)) {
  537. return -EINVAL;
  538. }
  539. cnt += c->limits[j].max;
  540. /*
  541. * Don't advertise an unsupported type
  542. * in a combination.
  543. */
  544. if (WARN_ON((wiphy->interface_modes & types) != types))
  545. return -EINVAL;
  546. }
  547. if (WARN_ON(all_iftypes & BIT(NL80211_IFTYPE_WDS)))
  548. return -EINVAL;
  549. /* You can't even choose that many! */
  550. if (WARN_ON(cnt < c->max_interfaces))
  551. return -EINVAL;
  552. }
  553. return 0;
  554. }
  555. static int wiphy_verify_combinations(struct wiphy *wiphy)
  556. {
  557. int i, ret;
  558. bool combined_radio = false;
  559. if (wiphy->n_radio) {
  560. for (i = 0; i < wiphy->n_radio; i++) {
  561. const struct wiphy_radio *radio = &wiphy->radio[i];
  562. ret = wiphy_verify_iface_combinations(wiphy,
  563. radio->iface_combinations,
  564. radio->n_iface_combinations,
  565. false);
  566. if (ret)
  567. return ret;
  568. }
  569. combined_radio = true;
  570. }
  571. ret = wiphy_verify_iface_combinations(wiphy,
  572. wiphy->iface_combinations,
  573. wiphy->n_iface_combinations,
  574. combined_radio);
  575. return ret;
  576. }
  577. int wiphy_register(struct wiphy *wiphy)
  578. {
  579. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  580. int res;
  581. enum nl80211_band band;
  582. struct ieee80211_supported_band *sband;
  583. bool have_band = false;
  584. int i;
  585. u16 ifmodes = wiphy->interface_modes;
  586. #ifdef CONFIG_PM
  587. if (WARN_ON(wiphy->wowlan &&
  588. (wiphy->wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
  589. !(wiphy->wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY)))
  590. return -EINVAL;
  591. if (WARN_ON(wiphy->wowlan &&
  592. !wiphy->wowlan->flags && !wiphy->wowlan->n_patterns &&
  593. !wiphy->wowlan->tcp))
  594. return -EINVAL;
  595. #endif
  596. if (WARN_ON((wiphy->features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH) &&
  597. (!rdev->ops->tdls_channel_switch ||
  598. !rdev->ops->tdls_cancel_channel_switch)))
  599. return -EINVAL;
  600. if (WARN_ON((wiphy->interface_modes & BIT(NL80211_IFTYPE_NAN)) &&
  601. (!rdev->ops->start_nan || !rdev->ops->stop_nan ||
  602. !rdev->ops->add_nan_func || !rdev->ops->del_nan_func ||
  603. !(wiphy->nan_supported_bands & BIT(NL80211_BAND_2GHZ)))))
  604. return -EINVAL;
  605. if (WARN_ON(wiphy->interface_modes & BIT(NL80211_IFTYPE_WDS)))
  606. return -EINVAL;
  607. if (WARN_ON(wiphy->pmsr_capa && !wiphy->pmsr_capa->ftm.supported))
  608. return -EINVAL;
  609. if (wiphy->pmsr_capa && wiphy->pmsr_capa->ftm.supported) {
  610. if (WARN_ON(!wiphy->pmsr_capa->ftm.asap &&
  611. !wiphy->pmsr_capa->ftm.non_asap))
  612. return -EINVAL;
  613. if (WARN_ON(!wiphy->pmsr_capa->ftm.preambles ||
  614. !wiphy->pmsr_capa->ftm.bandwidths))
  615. return -EINVAL;
  616. if (WARN_ON(wiphy->pmsr_capa->ftm.preambles &
  617. ~(BIT(NL80211_PREAMBLE_LEGACY) |
  618. BIT(NL80211_PREAMBLE_HT) |
  619. BIT(NL80211_PREAMBLE_VHT) |
  620. BIT(NL80211_PREAMBLE_HE) |
  621. BIT(NL80211_PREAMBLE_DMG))))
  622. return -EINVAL;
  623. if (WARN_ON((wiphy->pmsr_capa->ftm.trigger_based ||
  624. wiphy->pmsr_capa->ftm.non_trigger_based) &&
  625. !(wiphy->pmsr_capa->ftm.preambles &
  626. BIT(NL80211_PREAMBLE_HE))))
  627. return -EINVAL;
  628. if (WARN_ON(wiphy->pmsr_capa->ftm.bandwidths &
  629. ~(BIT(NL80211_CHAN_WIDTH_20_NOHT) |
  630. BIT(NL80211_CHAN_WIDTH_20) |
  631. BIT(NL80211_CHAN_WIDTH_40) |
  632. BIT(NL80211_CHAN_WIDTH_80) |
  633. BIT(NL80211_CHAN_WIDTH_80P80) |
  634. BIT(NL80211_CHAN_WIDTH_160) |
  635. BIT(NL80211_CHAN_WIDTH_320) |
  636. BIT(NL80211_CHAN_WIDTH_5) |
  637. BIT(NL80211_CHAN_WIDTH_10))))
  638. return -EINVAL;
  639. }
  640. if (WARN_ON((wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) &&
  641. (wiphy->regulatory_flags &
  642. (REGULATORY_CUSTOM_REG |
  643. REGULATORY_STRICT_REG |
  644. REGULATORY_COUNTRY_IE_FOLLOW_POWER |
  645. REGULATORY_COUNTRY_IE_IGNORE))))
  646. return -EINVAL;
  647. if (WARN_ON(wiphy->coalesce &&
  648. (!wiphy->coalesce->n_rules ||
  649. !wiphy->coalesce->n_patterns) &&
  650. (!wiphy->coalesce->pattern_min_len ||
  651. wiphy->coalesce->pattern_min_len >
  652. wiphy->coalesce->pattern_max_len)))
  653. return -EINVAL;
  654. if (WARN_ON(wiphy->ap_sme_capa &&
  655. !(wiphy->flags & WIPHY_FLAG_HAVE_AP_SME)))
  656. return -EINVAL;
  657. if (WARN_ON(wiphy->addresses && !wiphy->n_addresses))
  658. return -EINVAL;
  659. if (WARN_ON(wiphy->addresses &&
  660. !is_zero_ether_addr(wiphy->perm_addr) &&
  661. memcmp(wiphy->perm_addr, wiphy->addresses[0].addr,
  662. ETH_ALEN)))
  663. return -EINVAL;
  664. if (WARN_ON(wiphy->max_acl_mac_addrs &&
  665. (!(wiphy->flags & WIPHY_FLAG_HAVE_AP_SME) ||
  666. !rdev->ops->set_mac_acl)))
  667. return -EINVAL;
  668. /* assure only valid behaviours are flagged by driver
  669. * hence subtract 2 as bit 0 is invalid.
  670. */
  671. if (WARN_ON(wiphy->bss_select_support &&
  672. (wiphy->bss_select_support & ~(BIT(__NL80211_BSS_SELECT_ATTR_AFTER_LAST) - 2))))
  673. return -EINVAL;
  674. if (WARN_ON(wiphy_ext_feature_isset(&rdev->wiphy,
  675. NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X) &&
  676. (!rdev->ops->set_pmk || !rdev->ops->del_pmk)))
  677. return -EINVAL;
  678. if (WARN_ON(!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
  679. rdev->ops->update_connect_params))
  680. return -EINVAL;
  681. if (wiphy->addresses)
  682. memcpy(wiphy->perm_addr, wiphy->addresses[0].addr, ETH_ALEN);
  683. /* sanity check ifmodes */
  684. WARN_ON(!ifmodes);
  685. ifmodes &= ((1 << NUM_NL80211_IFTYPES) - 1) & ~1;
  686. if (WARN_ON(ifmodes != wiphy->interface_modes))
  687. wiphy->interface_modes = ifmodes;
  688. res = wiphy_verify_combinations(wiphy);
  689. if (res)
  690. return res;
  691. /* sanity check supported bands/channels */
  692. for (band = 0; band < NUM_NL80211_BANDS; band++) {
  693. const struct ieee80211_sband_iftype_data *iftd;
  694. u16 types = 0;
  695. bool have_he = false;
  696. sband = wiphy->bands[band];
  697. if (!sband)
  698. continue;
  699. sband->band = band;
  700. if (WARN_ON(!sband->n_channels))
  701. return -EINVAL;
  702. /*
  703. * on 60GHz or sub-1Ghz band, there are no legacy rates, so
  704. * n_bitrates is 0
  705. */
  706. if (WARN_ON((band != NL80211_BAND_60GHZ &&
  707. band != NL80211_BAND_S1GHZ) &&
  708. !sband->n_bitrates))
  709. return -EINVAL;
  710. if (WARN_ON(band == NL80211_BAND_6GHZ &&
  711. (sband->ht_cap.ht_supported ||
  712. sband->vht_cap.vht_supported)))
  713. return -EINVAL;
  714. /*
  715. * Since cfg80211_disable_40mhz_24ghz is global, we can
  716. * modify the sband's ht data even if the driver uses a
  717. * global structure for that.
  718. */
  719. if (cfg80211_disable_40mhz_24ghz &&
  720. band == NL80211_BAND_2GHZ &&
  721. sband->ht_cap.ht_supported) {
  722. sband->ht_cap.cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  723. sband->ht_cap.cap &= ~IEEE80211_HT_CAP_SGI_40;
  724. }
  725. /*
  726. * Since we use a u32 for rate bitmaps in
  727. * ieee80211_get_response_rate, we cannot
  728. * have more than 32 legacy rates.
  729. */
  730. if (WARN_ON(sband->n_bitrates > 32))
  731. return -EINVAL;
  732. for (i = 0; i < sband->n_channels; i++) {
  733. sband->channels[i].orig_flags =
  734. sband->channels[i].flags;
  735. sband->channels[i].orig_mag = INT_MAX;
  736. sband->channels[i].orig_mpwr =
  737. sband->channels[i].max_power;
  738. sband->channels[i].band = band;
  739. if (WARN_ON(sband->channels[i].freq_offset >= 1000))
  740. return -EINVAL;
  741. }
  742. for_each_sband_iftype_data(sband, i, iftd) {
  743. bool has_ap, has_non_ap;
  744. u32 ap_bits = BIT(NL80211_IFTYPE_AP) |
  745. BIT(NL80211_IFTYPE_P2P_GO);
  746. if (WARN_ON(!iftd->types_mask))
  747. return -EINVAL;
  748. if (WARN_ON(types & iftd->types_mask))
  749. return -EINVAL;
  750. /* at least one piece of information must be present */
  751. if (WARN_ON(!iftd->he_cap.has_he))
  752. return -EINVAL;
  753. types |= iftd->types_mask;
  754. if (i == 0)
  755. have_he = iftd->he_cap.has_he;
  756. else
  757. have_he = have_he &&
  758. iftd->he_cap.has_he;
  759. has_ap = iftd->types_mask & ap_bits;
  760. has_non_ap = iftd->types_mask & ~ap_bits;
  761. /*
  762. * For EHT 20 MHz STA, the capabilities format differs
  763. * but to simplify, don't check 20 MHz but rather check
  764. * only if AP and non-AP were mentioned at the same time,
  765. * reject if so.
  766. */
  767. if (WARN_ON(iftd->eht_cap.has_eht &&
  768. has_ap && has_non_ap))
  769. return -EINVAL;
  770. }
  771. if (WARN_ON(!have_he && band == NL80211_BAND_6GHZ))
  772. return -EINVAL;
  773. have_band = true;
  774. }
  775. if (!have_band) {
  776. WARN_ON(1);
  777. return -EINVAL;
  778. }
  779. for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
  780. /*
  781. * Validate we have a policy (can be explicitly set to
  782. * VENDOR_CMD_RAW_DATA which is non-NULL) and also that
  783. * we have at least one of doit/dumpit.
  784. */
  785. if (WARN_ON(!rdev->wiphy.vendor_commands[i].policy))
  786. return -EINVAL;
  787. if (WARN_ON(!rdev->wiphy.vendor_commands[i].doit &&
  788. !rdev->wiphy.vendor_commands[i].dumpit))
  789. return -EINVAL;
  790. }
  791. #ifdef CONFIG_PM
  792. if (WARN_ON(rdev->wiphy.wowlan && rdev->wiphy.wowlan->n_patterns &&
  793. (!rdev->wiphy.wowlan->pattern_min_len ||
  794. rdev->wiphy.wowlan->pattern_min_len >
  795. rdev->wiphy.wowlan->pattern_max_len)))
  796. return -EINVAL;
  797. #endif
  798. if (!wiphy->max_num_akm_suites)
  799. wiphy->max_num_akm_suites = NL80211_MAX_NR_AKM_SUITES;
  800. else if (wiphy->max_num_akm_suites < NL80211_MAX_NR_AKM_SUITES ||
  801. wiphy->max_num_akm_suites > CFG80211_MAX_NUM_AKM_SUITES)
  802. return -EINVAL;
  803. /* Allocate radio configuration space for multi-radio wiphy */
  804. if (wiphy->n_radio > 0) {
  805. int idx;
  806. wiphy->radio_cfg = kzalloc_objs(*wiphy->radio_cfg,
  807. wiphy->n_radio);
  808. if (!wiphy->radio_cfg)
  809. return -ENOMEM;
  810. /*
  811. * Initialize wiphy radio parameters to IEEE 802.11
  812. * MIB default values. RTS threshold is disabled by
  813. * default with the special -1 value.
  814. */
  815. for (idx = 0; idx < wiphy->n_radio; idx++)
  816. wiphy->radio_cfg[idx].rts_threshold = (u32)-1;
  817. }
  818. /* check and set up bitrates */
  819. ieee80211_set_bitrate_flags(wiphy);
  820. rdev->wiphy.features |= NL80211_FEATURE_SCAN_FLUSH;
  821. if (rdev->wiphy.bss_param_support & WIPHY_BSS_PARAM_P2P_CTWINDOW)
  822. rdev->wiphy.features |= NL80211_FEATURE_P2P_GO_CTWIN;
  823. else if (rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN)
  824. rdev->wiphy.bss_param_support |= WIPHY_BSS_PARAM_P2P_CTWINDOW;
  825. if (rdev->wiphy.bss_param_support & WIPHY_BSS_PARAM_P2P_OPPPS)
  826. rdev->wiphy.features |= NL80211_FEATURE_P2P_GO_OPPPS;
  827. else if (rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS)
  828. rdev->wiphy.bss_param_support |= WIPHY_BSS_PARAM_P2P_OPPPS;
  829. rtnl_lock();
  830. wiphy_lock(&rdev->wiphy);
  831. res = device_add(&rdev->wiphy.dev);
  832. if (res) {
  833. wiphy_unlock(&rdev->wiphy);
  834. rtnl_unlock();
  835. return res;
  836. }
  837. list_add_rcu(&rdev->list, &cfg80211_rdev_list);
  838. cfg80211_rdev_list_generation++;
  839. /* add to debugfs */
  840. rdev->wiphy.debugfsdir = debugfs_create_dir(wiphy_name(&rdev->wiphy),
  841. ieee80211_debugfs_dir);
  842. if (wiphy->n_radio > 0) {
  843. int idx;
  844. char radio_name[RADIO_DEBUGFSDIR_MAX_LEN];
  845. for (idx = 0; idx < wiphy->n_radio; idx++) {
  846. scnprintf(radio_name, sizeof(radio_name), "radio%d",
  847. idx);
  848. wiphy->radio_cfg[idx].radio_debugfsdir =
  849. debugfs_create_dir(radio_name,
  850. rdev->wiphy.debugfsdir);
  851. }
  852. }
  853. cfg80211_debugfs_rdev_add(rdev);
  854. nl80211_notify_wiphy(rdev, NL80211_CMD_NEW_WIPHY);
  855. wiphy_unlock(&rdev->wiphy);
  856. /* set up regulatory info */
  857. wiphy_regulatory_register(wiphy);
  858. if (wiphy->regulatory_flags & REGULATORY_CUSTOM_REG) {
  859. struct regulatory_request request = {
  860. .wiphy_idx = get_wiphy_idx(wiphy),
  861. .initiator = NL80211_REGDOM_SET_BY_DRIVER,
  862. .alpha2[0] = '9',
  863. .alpha2[1] = '9',
  864. };
  865. nl80211_send_reg_change_event(&request);
  866. }
  867. /* Check that nobody globally advertises any capabilities they do not
  868. * advertise on all possible interface types.
  869. */
  870. if (wiphy->extended_capabilities_len &&
  871. wiphy->num_iftype_ext_capab &&
  872. wiphy->iftype_ext_capab) {
  873. u8 supported_on_all, j;
  874. const struct wiphy_iftype_ext_capab *capab;
  875. capab = wiphy->iftype_ext_capab;
  876. for (j = 0; j < wiphy->extended_capabilities_len; j++) {
  877. if (capab[0].extended_capabilities_len > j)
  878. supported_on_all =
  879. capab[0].extended_capabilities[j];
  880. else
  881. supported_on_all = 0x00;
  882. for (i = 1; i < wiphy->num_iftype_ext_capab; i++) {
  883. if (j >= capab[i].extended_capabilities_len) {
  884. supported_on_all = 0x00;
  885. break;
  886. }
  887. supported_on_all &=
  888. capab[i].extended_capabilities[j];
  889. }
  890. if (WARN_ON(wiphy->extended_capabilities[j] &
  891. ~supported_on_all))
  892. break;
  893. }
  894. }
  895. rdev->wiphy.registered = true;
  896. rtnl_unlock();
  897. res = rfkill_register(rdev->wiphy.rfkill);
  898. if (res) {
  899. rfkill_destroy(rdev->wiphy.rfkill);
  900. rdev->wiphy.rfkill = NULL;
  901. wiphy_unregister(&rdev->wiphy);
  902. return res;
  903. }
  904. return 0;
  905. }
  906. EXPORT_SYMBOL(wiphy_register);
  907. void wiphy_rfkill_start_polling(struct wiphy *wiphy)
  908. {
  909. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  910. if (!rdev->ops->rfkill_poll)
  911. return;
  912. rdev->rfkill_ops.poll = cfg80211_rfkill_poll;
  913. rfkill_resume_polling(wiphy->rfkill);
  914. }
  915. EXPORT_SYMBOL(wiphy_rfkill_start_polling);
  916. void cfg80211_process_wiphy_works(struct cfg80211_registered_device *rdev,
  917. struct wiphy_work *end)
  918. {
  919. unsigned int runaway_limit = 100;
  920. unsigned long flags;
  921. lockdep_assert_held(&rdev->wiphy.mtx);
  922. spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
  923. while (!list_empty(&rdev->wiphy_work_list)) {
  924. struct wiphy_work *wk;
  925. wk = list_first_entry(&rdev->wiphy_work_list,
  926. struct wiphy_work, entry);
  927. list_del_init(&wk->entry);
  928. spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
  929. trace_wiphy_work_run(&rdev->wiphy, wk);
  930. wk->func(&rdev->wiphy, wk);
  931. spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
  932. if (wk == end)
  933. break;
  934. if (WARN_ON(--runaway_limit == 0))
  935. INIT_LIST_HEAD(&rdev->wiphy_work_list);
  936. }
  937. spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
  938. }
  939. void wiphy_unregister(struct wiphy *wiphy)
  940. {
  941. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  942. wait_event(rdev->dev_wait, ({
  943. int __count;
  944. wiphy_lock(&rdev->wiphy);
  945. __count = rdev->opencount;
  946. wiphy_unlock(&rdev->wiphy);
  947. __count == 0; }));
  948. if (rdev->wiphy.rfkill)
  949. rfkill_unregister(rdev->wiphy.rfkill);
  950. rtnl_lock();
  951. wiphy_lock(&rdev->wiphy);
  952. nl80211_notify_wiphy(rdev, NL80211_CMD_DEL_WIPHY);
  953. rdev->wiphy.registered = false;
  954. WARN_ON(!list_empty(&rdev->wiphy.wdev_list));
  955. /*
  956. * First remove the hardware from everywhere, this makes
  957. * it impossible to find from userspace.
  958. */
  959. debugfs_remove_recursive(rdev->wiphy.debugfsdir);
  960. list_del_rcu(&rdev->list);
  961. synchronize_rcu();
  962. /*
  963. * If this device got a regulatory hint tell core its
  964. * free to listen now to a new shiny device regulatory hint
  965. */
  966. wiphy_regulatory_deregister(wiphy);
  967. cfg80211_rdev_list_generation++;
  968. device_del(&rdev->wiphy.dev);
  969. #ifdef CONFIG_PM
  970. if (rdev->wiphy.wowlan_config && rdev->ops->set_wakeup)
  971. rdev_set_wakeup(rdev, false);
  972. #endif
  973. /* surely nothing is reachable now, clean up work */
  974. cfg80211_process_wiphy_works(rdev, NULL);
  975. wiphy_unlock(&rdev->wiphy);
  976. rtnl_unlock();
  977. /* this has nothing to do now but make sure it's gone */
  978. cancel_work_sync(&rdev->wiphy_work);
  979. cancel_work_sync(&rdev->rfkill_block);
  980. cancel_work_sync(&rdev->conn_work);
  981. flush_work(&rdev->event_work);
  982. cancel_delayed_work_sync(&rdev->dfs_update_channels_wk);
  983. cancel_delayed_work_sync(&rdev->background_cac_done_wk);
  984. flush_work(&rdev->destroy_work);
  985. flush_work(&rdev->propagate_radar_detect_wk);
  986. flush_work(&rdev->propagate_cac_done_wk);
  987. flush_work(&rdev->mgmt_registrations_update_wk);
  988. flush_work(&rdev->background_cac_abort_wk);
  989. cfg80211_rdev_free_wowlan(rdev);
  990. cfg80211_free_coalesce(rdev->coalesce);
  991. rdev->coalesce = NULL;
  992. }
  993. EXPORT_SYMBOL(wiphy_unregister);
  994. void cfg80211_dev_free(struct cfg80211_registered_device *rdev)
  995. {
  996. struct cfg80211_internal_bss *scan, *tmp;
  997. struct cfg80211_beacon_registration *reg, *treg;
  998. unsigned long flags;
  999. spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
  1000. WARN_ON(!list_empty(&rdev->wiphy_work_list));
  1001. spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
  1002. cancel_work_sync(&rdev->wiphy_work);
  1003. rfkill_destroy(rdev->wiphy.rfkill);
  1004. list_for_each_entry_safe(reg, treg, &rdev->beacon_registrations, list) {
  1005. list_del(&reg->list);
  1006. kfree(reg);
  1007. }
  1008. list_for_each_entry_safe(scan, tmp, &rdev->bss_list, list)
  1009. cfg80211_put_bss(&rdev->wiphy, &scan->pub);
  1010. mutex_destroy(&rdev->wiphy.mtx);
  1011. /*
  1012. * The 'regd' can only be non-NULL if we never finished
  1013. * initializing the wiphy and thus never went through the
  1014. * unregister path - e.g. in failure scenarios. Thus, it
  1015. * cannot have been visible to anyone if non-NULL, so we
  1016. * can just free it here.
  1017. */
  1018. kfree(rcu_dereference_raw(rdev->wiphy.regd));
  1019. kfree(rdev);
  1020. }
  1021. void wiphy_free(struct wiphy *wiphy)
  1022. {
  1023. kfree(wiphy->radio_cfg);
  1024. put_device(&wiphy->dev);
  1025. }
  1026. EXPORT_SYMBOL(wiphy_free);
  1027. void wiphy_rfkill_set_hw_state_reason(struct wiphy *wiphy, bool blocked,
  1028. enum rfkill_hard_block_reasons reason)
  1029. {
  1030. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  1031. if (rfkill_set_hw_state_reason(wiphy->rfkill, blocked, reason))
  1032. schedule_work(&rdev->rfkill_block);
  1033. }
  1034. EXPORT_SYMBOL(wiphy_rfkill_set_hw_state_reason);
  1035. static void _cfg80211_unregister_wdev(struct wireless_dev *wdev,
  1036. bool unregister_netdev)
  1037. {
  1038. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  1039. struct cfg80211_cqm_config *cqm_config;
  1040. unsigned int link_id;
  1041. ASSERT_RTNL();
  1042. lockdep_assert_held(&rdev->wiphy.mtx);
  1043. nl80211_notify_iface(rdev, wdev, NL80211_CMD_DEL_INTERFACE);
  1044. wdev->registered = false;
  1045. if (wdev->netdev) {
  1046. sysfs_remove_link(&wdev->netdev->dev.kobj, "phy80211");
  1047. if (unregister_netdev)
  1048. unregister_netdevice(wdev->netdev);
  1049. }
  1050. list_del_rcu(&wdev->list);
  1051. synchronize_net();
  1052. rdev->devlist_generation++;
  1053. cfg80211_mlme_purge_registrations(wdev);
  1054. switch (wdev->iftype) {
  1055. case NL80211_IFTYPE_P2P_DEVICE:
  1056. cfg80211_stop_p2p_device(rdev, wdev);
  1057. break;
  1058. case NL80211_IFTYPE_NAN:
  1059. cfg80211_stop_nan(rdev, wdev);
  1060. break;
  1061. default:
  1062. break;
  1063. }
  1064. #ifdef CONFIG_CFG80211_WEXT
  1065. kfree_sensitive(wdev->wext.keys);
  1066. wdev->wext.keys = NULL;
  1067. #endif
  1068. wiphy_work_cancel(wdev->wiphy, &wdev->cqm_rssi_work);
  1069. /* deleted from the list, so can't be found from nl80211 any more */
  1070. cqm_config = rcu_access_pointer(wdev->cqm_config);
  1071. kfree_rcu(cqm_config, rcu_head);
  1072. RCU_INIT_POINTER(wdev->cqm_config, NULL);
  1073. /*
  1074. * Ensure that all events have been processed and
  1075. * freed.
  1076. */
  1077. cfg80211_process_wdev_events(wdev);
  1078. if (wdev->iftype == NL80211_IFTYPE_STATION ||
  1079. wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) {
  1080. for (link_id = 0; link_id < ARRAY_SIZE(wdev->links); link_id++) {
  1081. struct cfg80211_internal_bss *curbss;
  1082. curbss = wdev->links[link_id].client.current_bss;
  1083. if (WARN_ON(curbss)) {
  1084. cfg80211_unhold_bss(curbss);
  1085. cfg80211_put_bss(wdev->wiphy, &curbss->pub);
  1086. wdev->links[link_id].client.current_bss = NULL;
  1087. }
  1088. }
  1089. }
  1090. wdev->connected = false;
  1091. }
  1092. void cfg80211_unregister_wdev(struct wireless_dev *wdev)
  1093. {
  1094. _cfg80211_unregister_wdev(wdev, true);
  1095. }
  1096. EXPORT_SYMBOL(cfg80211_unregister_wdev);
  1097. static const struct device_type wiphy_type = {
  1098. .name = "wlan",
  1099. };
  1100. void cfg80211_update_iface_num(struct cfg80211_registered_device *rdev,
  1101. enum nl80211_iftype iftype, int num)
  1102. {
  1103. lockdep_assert_held(&rdev->wiphy.mtx);
  1104. rdev->num_running_ifaces += num;
  1105. if (iftype == NL80211_IFTYPE_MONITOR)
  1106. rdev->num_running_monitor_ifaces += num;
  1107. }
  1108. void cfg80211_leave(struct cfg80211_registered_device *rdev,
  1109. struct wireless_dev *wdev,
  1110. int link_id)
  1111. {
  1112. struct net_device *dev = wdev->netdev;
  1113. struct cfg80211_sched_scan_request *pos, *tmp;
  1114. lockdep_assert_held(&rdev->wiphy.mtx);
  1115. cfg80211_pmsr_wdev_down(wdev);
  1116. cfg80211_stop_radar_detection(wdev);
  1117. cfg80211_stop_background_radar_detection(wdev);
  1118. switch (wdev->iftype) {
  1119. case NL80211_IFTYPE_ADHOC:
  1120. cfg80211_leave_ibss(rdev, dev, true);
  1121. break;
  1122. case NL80211_IFTYPE_P2P_CLIENT:
  1123. case NL80211_IFTYPE_STATION:
  1124. list_for_each_entry_safe(pos, tmp, &rdev->sched_scan_req_list,
  1125. list) {
  1126. if (dev == pos->dev)
  1127. cfg80211_stop_sched_scan_req(rdev, pos, false);
  1128. }
  1129. #ifdef CONFIG_CFG80211_WEXT
  1130. kfree(wdev->wext.ie);
  1131. wdev->wext.ie = NULL;
  1132. wdev->wext.ie_len = 0;
  1133. wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
  1134. #endif
  1135. cfg80211_disconnect(rdev, dev,
  1136. WLAN_REASON_DEAUTH_LEAVING, true);
  1137. break;
  1138. case NL80211_IFTYPE_MESH_POINT:
  1139. cfg80211_leave_mesh(rdev, dev);
  1140. break;
  1141. case NL80211_IFTYPE_AP:
  1142. case NL80211_IFTYPE_P2P_GO:
  1143. cfg80211_stop_ap(rdev, dev, link_id, true);
  1144. break;
  1145. case NL80211_IFTYPE_OCB:
  1146. cfg80211_leave_ocb(rdev, dev);
  1147. break;
  1148. case NL80211_IFTYPE_P2P_DEVICE:
  1149. cfg80211_stop_p2p_device(rdev, wdev);
  1150. break;
  1151. case NL80211_IFTYPE_NAN:
  1152. cfg80211_stop_nan(rdev, wdev);
  1153. break;
  1154. case NL80211_IFTYPE_AP_VLAN:
  1155. case NL80211_IFTYPE_MONITOR:
  1156. /* nothing to do */
  1157. break;
  1158. case NL80211_IFTYPE_UNSPECIFIED:
  1159. case NL80211_IFTYPE_WDS:
  1160. case NUM_NL80211_IFTYPES:
  1161. /* invalid */
  1162. break;
  1163. }
  1164. }
  1165. void cfg80211_stop_link(struct wiphy *wiphy, struct wireless_dev *wdev,
  1166. int link_id, gfp_t gfp)
  1167. {
  1168. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  1169. struct cfg80211_event *ev;
  1170. unsigned long flags;
  1171. /* Only AP/GO interfaces may have a specific link_id */
  1172. if (WARN_ON_ONCE(link_id != -1 &&
  1173. wdev->iftype != NL80211_IFTYPE_AP &&
  1174. wdev->iftype != NL80211_IFTYPE_P2P_GO))
  1175. link_id = -1;
  1176. trace_cfg80211_stop_link(wiphy, wdev, link_id);
  1177. ev = kzalloc_obj(*ev, gfp);
  1178. if (!ev)
  1179. return;
  1180. ev->type = EVENT_STOPPED;
  1181. ev->link_id = link_id;
  1182. spin_lock_irqsave(&wdev->event_lock, flags);
  1183. list_add_tail(&ev->list, &wdev->event_list);
  1184. spin_unlock_irqrestore(&wdev->event_lock, flags);
  1185. queue_work(cfg80211_wq, &rdev->event_work);
  1186. }
  1187. EXPORT_SYMBOL(cfg80211_stop_link);
  1188. void cfg80211_init_wdev(struct wireless_dev *wdev)
  1189. {
  1190. INIT_LIST_HEAD(&wdev->event_list);
  1191. spin_lock_init(&wdev->event_lock);
  1192. INIT_LIST_HEAD(&wdev->mgmt_registrations);
  1193. INIT_LIST_HEAD(&wdev->pmsr_list);
  1194. spin_lock_init(&wdev->pmsr_lock);
  1195. INIT_WORK(&wdev->pmsr_free_wk, cfg80211_pmsr_free_wk);
  1196. #ifdef CONFIG_CFG80211_WEXT
  1197. wdev->wext.default_key = -1;
  1198. wdev->wext.default_mgmt_key = -1;
  1199. wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
  1200. #endif
  1201. wiphy_work_init(&wdev->cqm_rssi_work, cfg80211_cqm_rssi_notify_work);
  1202. if (wdev->wiphy->flags & WIPHY_FLAG_PS_ON_BY_DEFAULT)
  1203. wdev->ps = true;
  1204. else
  1205. wdev->ps = false;
  1206. /* allow mac80211 to determine the timeout */
  1207. wdev->ps_timeout = -1;
  1208. wdev->radio_mask = BIT(wdev->wiphy->n_radio) - 1;
  1209. if ((wdev->iftype == NL80211_IFTYPE_STATION ||
  1210. wdev->iftype == NL80211_IFTYPE_P2P_CLIENT ||
  1211. wdev->iftype == NL80211_IFTYPE_ADHOC) && !wdev->use_4addr)
  1212. wdev->netdev->priv_flags |= IFF_DONT_BRIDGE;
  1213. INIT_WORK(&wdev->disconnect_wk, cfg80211_autodisconnect_wk);
  1214. }
  1215. void cfg80211_register_wdev(struct cfg80211_registered_device *rdev,
  1216. struct wireless_dev *wdev)
  1217. {
  1218. ASSERT_RTNL();
  1219. lockdep_assert_held(&rdev->wiphy.mtx);
  1220. /*
  1221. * We get here also when the interface changes network namespaces,
  1222. * as it's registered into the new one, but we don't want it to
  1223. * change ID in that case. Checking if the ID is already assigned
  1224. * works, because 0 isn't considered a valid ID and the memory is
  1225. * 0-initialized.
  1226. */
  1227. if (!wdev->identifier)
  1228. wdev->identifier = ++rdev->wdev_id;
  1229. list_add_rcu(&wdev->list, &rdev->wiphy.wdev_list);
  1230. rdev->devlist_generation++;
  1231. wdev->registered = true;
  1232. if (wdev->netdev &&
  1233. sysfs_create_link(&wdev->netdev->dev.kobj, &rdev->wiphy.dev.kobj,
  1234. "phy80211"))
  1235. pr_err("failed to add phy80211 symlink to netdev!\n");
  1236. nl80211_notify_iface(rdev, wdev, NL80211_CMD_NEW_INTERFACE);
  1237. }
  1238. int cfg80211_register_netdevice(struct net_device *dev)
  1239. {
  1240. struct wireless_dev *wdev = dev->ieee80211_ptr;
  1241. struct cfg80211_registered_device *rdev;
  1242. int ret;
  1243. ASSERT_RTNL();
  1244. if (WARN_ON(!wdev))
  1245. return -EINVAL;
  1246. rdev = wiphy_to_rdev(wdev->wiphy);
  1247. lockdep_assert_held(&rdev->wiphy.mtx);
  1248. /* we'll take care of this */
  1249. wdev->registered = true;
  1250. wdev->registering = true;
  1251. ret = register_netdevice(dev);
  1252. if (ret)
  1253. goto out;
  1254. cfg80211_register_wdev(rdev, wdev);
  1255. ret = 0;
  1256. out:
  1257. wdev->registering = false;
  1258. if (ret)
  1259. wdev->registered = false;
  1260. return ret;
  1261. }
  1262. EXPORT_SYMBOL(cfg80211_register_netdevice);
  1263. static int cfg80211_netdev_notifier_call(struct notifier_block *nb,
  1264. unsigned long state, void *ptr)
  1265. {
  1266. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1267. struct wireless_dev *wdev = dev->ieee80211_ptr;
  1268. struct cfg80211_registered_device *rdev;
  1269. struct cfg80211_sched_scan_request *pos, *tmp;
  1270. if (!wdev)
  1271. return NOTIFY_DONE;
  1272. rdev = wiphy_to_rdev(wdev->wiphy);
  1273. WARN_ON(wdev->iftype == NL80211_IFTYPE_UNSPECIFIED);
  1274. switch (state) {
  1275. case NETDEV_POST_INIT:
  1276. SET_NETDEV_DEVTYPE(dev, &wiphy_type);
  1277. wdev->netdev = dev;
  1278. /* can only change netns with wiphy */
  1279. dev->netns_immutable = true;
  1280. cfg80211_init_wdev(wdev);
  1281. break;
  1282. case NETDEV_REGISTER:
  1283. if (!wdev->registered) {
  1284. guard(wiphy)(&rdev->wiphy);
  1285. cfg80211_register_wdev(rdev, wdev);
  1286. }
  1287. break;
  1288. case NETDEV_UNREGISTER:
  1289. /*
  1290. * It is possible to get NETDEV_UNREGISTER multiple times,
  1291. * so check wdev->registered.
  1292. */
  1293. if (wdev->registered && !wdev->registering) {
  1294. guard(wiphy)(&rdev->wiphy);
  1295. _cfg80211_unregister_wdev(wdev, false);
  1296. }
  1297. break;
  1298. case NETDEV_GOING_DOWN:
  1299. scoped_guard(wiphy, &rdev->wiphy) {
  1300. cfg80211_leave(rdev, wdev, -1);
  1301. cfg80211_remove_links(wdev);
  1302. }
  1303. /* since we just did cfg80211_leave() nothing to do there */
  1304. cancel_work_sync(&wdev->disconnect_wk);
  1305. cancel_work_sync(&wdev->pmsr_free_wk);
  1306. break;
  1307. case NETDEV_DOWN:
  1308. wiphy_lock(&rdev->wiphy);
  1309. cfg80211_update_iface_num(rdev, wdev->iftype, -1);
  1310. if (rdev->scan_req && rdev->scan_req->req.wdev == wdev) {
  1311. if (WARN_ON(!rdev->scan_req->notified &&
  1312. (!rdev->int_scan_req ||
  1313. !rdev->int_scan_req->notified)))
  1314. rdev->scan_req->info.aborted = true;
  1315. ___cfg80211_scan_done(rdev, false);
  1316. }
  1317. list_for_each_entry_safe(pos, tmp,
  1318. &rdev->sched_scan_req_list, list) {
  1319. if (WARN_ON(pos->dev == wdev->netdev))
  1320. cfg80211_stop_sched_scan_req(rdev, pos, false);
  1321. }
  1322. rdev->opencount--;
  1323. wiphy_unlock(&rdev->wiphy);
  1324. wake_up(&rdev->dev_wait);
  1325. break;
  1326. case NETDEV_UP:
  1327. wiphy_lock(&rdev->wiphy);
  1328. cfg80211_update_iface_num(rdev, wdev->iftype, 1);
  1329. switch (wdev->iftype) {
  1330. #ifdef CONFIG_CFG80211_WEXT
  1331. case NL80211_IFTYPE_ADHOC:
  1332. cfg80211_ibss_wext_join(rdev, wdev);
  1333. break;
  1334. case NL80211_IFTYPE_STATION:
  1335. cfg80211_mgd_wext_connect(rdev, wdev);
  1336. break;
  1337. #endif
  1338. #ifdef CONFIG_MAC80211_MESH
  1339. case NL80211_IFTYPE_MESH_POINT:
  1340. {
  1341. /* backward compat code... */
  1342. struct mesh_setup setup;
  1343. memcpy(&setup, &default_mesh_setup,
  1344. sizeof(setup));
  1345. /* back compat only needed for mesh_id */
  1346. setup.mesh_id = wdev->u.mesh.id;
  1347. setup.mesh_id_len = wdev->u.mesh.id_up_len;
  1348. if (wdev->u.mesh.id_up_len)
  1349. __cfg80211_join_mesh(rdev, dev,
  1350. &setup,
  1351. &default_mesh_config);
  1352. break;
  1353. }
  1354. #endif
  1355. default:
  1356. break;
  1357. }
  1358. rdev->opencount++;
  1359. /*
  1360. * Configure power management to the driver here so that its
  1361. * correctly set also after interface type changes etc.
  1362. */
  1363. if ((wdev->iftype == NL80211_IFTYPE_STATION ||
  1364. wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) &&
  1365. rdev->ops->set_power_mgmt &&
  1366. rdev_set_power_mgmt(rdev, dev, wdev->ps,
  1367. wdev->ps_timeout)) {
  1368. /* assume this means it's off */
  1369. wdev->ps = false;
  1370. }
  1371. wiphy_unlock(&rdev->wiphy);
  1372. break;
  1373. case NETDEV_PRE_UP:
  1374. if (!cfg80211_iftype_allowed(wdev->wiphy, wdev->iftype,
  1375. wdev->use_4addr, 0))
  1376. return notifier_from_errno(-EOPNOTSUPP);
  1377. if (rfkill_blocked(rdev->wiphy.rfkill))
  1378. return notifier_from_errno(-ERFKILL);
  1379. break;
  1380. default:
  1381. return NOTIFY_DONE;
  1382. }
  1383. wireless_nlevent_flush();
  1384. return NOTIFY_OK;
  1385. }
  1386. static struct notifier_block cfg80211_netdev_notifier = {
  1387. .notifier_call = cfg80211_netdev_notifier_call,
  1388. };
  1389. static void __net_exit cfg80211_pernet_exit(struct net *net)
  1390. {
  1391. struct cfg80211_registered_device *rdev;
  1392. rtnl_lock();
  1393. for_each_rdev(rdev) {
  1394. if (net_eq(wiphy_net(&rdev->wiphy), net))
  1395. WARN_ON(cfg80211_switch_netns(rdev, &init_net));
  1396. }
  1397. rtnl_unlock();
  1398. }
  1399. static struct pernet_operations cfg80211_pernet_ops = {
  1400. .exit = cfg80211_pernet_exit,
  1401. };
  1402. void wiphy_work_queue(struct wiphy *wiphy, struct wiphy_work *work)
  1403. {
  1404. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  1405. unsigned long flags;
  1406. trace_wiphy_work_queue(wiphy, work);
  1407. spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
  1408. if (list_empty(&work->entry))
  1409. list_add_tail(&work->entry, &rdev->wiphy_work_list);
  1410. spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
  1411. queue_work(system_dfl_wq, &rdev->wiphy_work);
  1412. }
  1413. EXPORT_SYMBOL_GPL(wiphy_work_queue);
  1414. void wiphy_work_cancel(struct wiphy *wiphy, struct wiphy_work *work)
  1415. {
  1416. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  1417. unsigned long flags;
  1418. lockdep_assert_held(&wiphy->mtx);
  1419. trace_wiphy_work_cancel(wiphy, work);
  1420. spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
  1421. if (!list_empty(&work->entry))
  1422. list_del_init(&work->entry);
  1423. spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
  1424. }
  1425. EXPORT_SYMBOL_GPL(wiphy_work_cancel);
  1426. void wiphy_work_flush(struct wiphy *wiphy, struct wiphy_work *work)
  1427. {
  1428. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  1429. unsigned long flags;
  1430. bool run;
  1431. trace_wiphy_work_flush(wiphy, work);
  1432. spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
  1433. run = !work || !list_empty(&work->entry);
  1434. spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
  1435. if (run)
  1436. cfg80211_process_wiphy_works(rdev, work);
  1437. }
  1438. EXPORT_SYMBOL_GPL(wiphy_work_flush);
  1439. void wiphy_delayed_work_timer(struct timer_list *t)
  1440. {
  1441. struct wiphy_delayed_work *dwork = timer_container_of(dwork, t, timer);
  1442. wiphy_work_queue(dwork->wiphy, &dwork->work);
  1443. }
  1444. EXPORT_SYMBOL(wiphy_delayed_work_timer);
  1445. void wiphy_delayed_work_queue(struct wiphy *wiphy,
  1446. struct wiphy_delayed_work *dwork,
  1447. unsigned long delay)
  1448. {
  1449. trace_wiphy_delayed_work_queue(wiphy, &dwork->work, delay);
  1450. if (!delay) {
  1451. timer_delete(&dwork->timer);
  1452. wiphy_work_queue(wiphy, &dwork->work);
  1453. return;
  1454. }
  1455. dwork->wiphy = wiphy;
  1456. mod_timer(&dwork->timer, jiffies + delay);
  1457. }
  1458. EXPORT_SYMBOL_GPL(wiphy_delayed_work_queue);
  1459. void wiphy_delayed_work_cancel(struct wiphy *wiphy,
  1460. struct wiphy_delayed_work *dwork)
  1461. {
  1462. lockdep_assert_held(&wiphy->mtx);
  1463. timer_delete_sync(&dwork->timer);
  1464. wiphy_work_cancel(wiphy, &dwork->work);
  1465. }
  1466. EXPORT_SYMBOL_GPL(wiphy_delayed_work_cancel);
  1467. void wiphy_delayed_work_flush(struct wiphy *wiphy,
  1468. struct wiphy_delayed_work *dwork)
  1469. {
  1470. lockdep_assert_held(&wiphy->mtx);
  1471. timer_delete_sync(&dwork->timer);
  1472. wiphy_work_flush(wiphy, &dwork->work);
  1473. }
  1474. EXPORT_SYMBOL_GPL(wiphy_delayed_work_flush);
  1475. bool wiphy_delayed_work_pending(struct wiphy *wiphy,
  1476. struct wiphy_delayed_work *dwork)
  1477. {
  1478. return timer_pending(&dwork->timer);
  1479. }
  1480. EXPORT_SYMBOL_GPL(wiphy_delayed_work_pending);
  1481. enum hrtimer_restart wiphy_hrtimer_work_timer(struct hrtimer *t)
  1482. {
  1483. struct wiphy_hrtimer_work *hrwork =
  1484. container_of(t, struct wiphy_hrtimer_work, timer);
  1485. wiphy_work_queue(hrwork->wiphy, &hrwork->work);
  1486. return HRTIMER_NORESTART;
  1487. }
  1488. EXPORT_SYMBOL_GPL(wiphy_hrtimer_work_timer);
  1489. void wiphy_hrtimer_work_queue(struct wiphy *wiphy,
  1490. struct wiphy_hrtimer_work *hrwork,
  1491. ktime_t delay)
  1492. {
  1493. trace_wiphy_hrtimer_work_queue(wiphy, &hrwork->work, delay);
  1494. if (!delay) {
  1495. hrtimer_cancel(&hrwork->timer);
  1496. wiphy_work_queue(wiphy, &hrwork->work);
  1497. return;
  1498. }
  1499. hrwork->wiphy = wiphy;
  1500. hrtimer_start_range_ns(&hrwork->timer, delay,
  1501. 1000 * NSEC_PER_USEC, HRTIMER_MODE_REL);
  1502. }
  1503. EXPORT_SYMBOL_GPL(wiphy_hrtimer_work_queue);
  1504. void wiphy_hrtimer_work_cancel(struct wiphy *wiphy,
  1505. struct wiphy_hrtimer_work *hrwork)
  1506. {
  1507. lockdep_assert_held(&wiphy->mtx);
  1508. hrtimer_cancel(&hrwork->timer);
  1509. wiphy_work_cancel(wiphy, &hrwork->work);
  1510. }
  1511. EXPORT_SYMBOL_GPL(wiphy_hrtimer_work_cancel);
  1512. void wiphy_hrtimer_work_flush(struct wiphy *wiphy,
  1513. struct wiphy_hrtimer_work *hrwork)
  1514. {
  1515. lockdep_assert_held(&wiphy->mtx);
  1516. hrtimer_cancel(&hrwork->timer);
  1517. wiphy_work_flush(wiphy, &hrwork->work);
  1518. }
  1519. EXPORT_SYMBOL_GPL(wiphy_hrtimer_work_flush);
  1520. bool wiphy_hrtimer_work_pending(struct wiphy *wiphy,
  1521. struct wiphy_hrtimer_work *hrwork)
  1522. {
  1523. return hrtimer_is_queued(&hrwork->timer);
  1524. }
  1525. EXPORT_SYMBOL_GPL(wiphy_hrtimer_work_pending);
  1526. static int __init cfg80211_init(void)
  1527. {
  1528. int err;
  1529. err = register_pernet_device(&cfg80211_pernet_ops);
  1530. if (err)
  1531. goto out_fail_pernet;
  1532. err = wiphy_sysfs_init();
  1533. if (err)
  1534. goto out_fail_sysfs;
  1535. err = register_netdevice_notifier(&cfg80211_netdev_notifier);
  1536. if (err)
  1537. goto out_fail_notifier;
  1538. err = nl80211_init();
  1539. if (err)
  1540. goto out_fail_nl80211;
  1541. ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
  1542. err = regulatory_init();
  1543. if (err)
  1544. goto out_fail_reg;
  1545. cfg80211_wq = alloc_ordered_workqueue("cfg80211", WQ_MEM_RECLAIM);
  1546. if (!cfg80211_wq) {
  1547. err = -ENOMEM;
  1548. goto out_fail_wq;
  1549. }
  1550. return 0;
  1551. out_fail_wq:
  1552. regulatory_exit();
  1553. out_fail_reg:
  1554. debugfs_remove(ieee80211_debugfs_dir);
  1555. nl80211_exit();
  1556. out_fail_nl80211:
  1557. unregister_netdevice_notifier(&cfg80211_netdev_notifier);
  1558. out_fail_notifier:
  1559. wiphy_sysfs_exit();
  1560. out_fail_sysfs:
  1561. unregister_pernet_device(&cfg80211_pernet_ops);
  1562. out_fail_pernet:
  1563. return err;
  1564. }
  1565. fs_initcall(cfg80211_init);
  1566. static void __exit cfg80211_exit(void)
  1567. {
  1568. debugfs_remove(ieee80211_debugfs_dir);
  1569. nl80211_exit();
  1570. unregister_netdevice_notifier(&cfg80211_netdev_notifier);
  1571. wiphy_sysfs_exit();
  1572. regulatory_exit();
  1573. unregister_pernet_device(&cfg80211_pernet_ops);
  1574. destroy_workqueue(cfg80211_wq);
  1575. }
  1576. module_exit(cfg80211_exit);