core.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  1. /*
  2. * This is the linux wireless configuration interface.
  3. *
  4. * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
  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. kfree(rdev);
  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. nl80211_notify_iface(rdev, wdev, NL80211_CMD_NEW_INTERFACE);
  921. break;
  922. case NETDEV_GOING_DOWN:
  923. cfg80211_leave(rdev, wdev);
  924. break;
  925. case NETDEV_DOWN:
  926. cfg80211_update_iface_num(rdev, wdev->iftype, -1);
  927. if (rdev->scan_req && rdev->scan_req->wdev == wdev) {
  928. if (WARN_ON(!rdev->scan_req->notified))
  929. rdev->scan_req->info.aborted = true;
  930. ___cfg80211_scan_done(rdev, false);
  931. }
  932. sched_scan_req = rtnl_dereference(rdev->sched_scan_req);
  933. if (WARN_ON(sched_scan_req &&
  934. sched_scan_req->dev == wdev->netdev)) {
  935. __cfg80211_stop_sched_scan(rdev, false);
  936. }
  937. rdev->opencount--;
  938. wake_up(&rdev->dev_wait);
  939. break;
  940. case NETDEV_UP:
  941. cfg80211_update_iface_num(rdev, wdev->iftype, 1);
  942. wdev_lock(wdev);
  943. switch (wdev->iftype) {
  944. #ifdef CONFIG_CFG80211_WEXT
  945. case NL80211_IFTYPE_ADHOC:
  946. cfg80211_ibss_wext_join(rdev, wdev);
  947. break;
  948. case NL80211_IFTYPE_STATION:
  949. cfg80211_mgd_wext_connect(rdev, wdev);
  950. break;
  951. #endif
  952. #ifdef CONFIG_MAC80211_MESH
  953. case NL80211_IFTYPE_MESH_POINT:
  954. {
  955. /* backward compat code... */
  956. struct mesh_setup setup;
  957. memcpy(&setup, &default_mesh_setup,
  958. sizeof(setup));
  959. /* back compat only needed for mesh_id */
  960. setup.mesh_id = wdev->ssid;
  961. setup.mesh_id_len = wdev->mesh_id_up_len;
  962. if (wdev->mesh_id_up_len)
  963. __cfg80211_join_mesh(rdev, dev,
  964. &setup,
  965. &default_mesh_config);
  966. break;
  967. }
  968. #endif
  969. default:
  970. break;
  971. }
  972. wdev_unlock(wdev);
  973. rdev->opencount++;
  974. /*
  975. * Configure power management to the driver here so that its
  976. * correctly set also after interface type changes etc.
  977. */
  978. if ((wdev->iftype == NL80211_IFTYPE_STATION ||
  979. wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) &&
  980. rdev->ops->set_power_mgmt)
  981. if (rdev_set_power_mgmt(rdev, dev, wdev->ps,
  982. wdev->ps_timeout)) {
  983. /* assume this means it's off */
  984. wdev->ps = false;
  985. }
  986. break;
  987. case NETDEV_UNREGISTER:
  988. /*
  989. * It is possible to get NETDEV_UNREGISTER
  990. * multiple times. To detect that, check
  991. * that the interface is still on the list
  992. * of registered interfaces, and only then
  993. * remove and clean it up.
  994. */
  995. if (!list_empty(&wdev->list)) {
  996. nl80211_notify_iface(rdev, wdev,
  997. NL80211_CMD_DEL_INTERFACE);
  998. sysfs_remove_link(&dev->dev.kobj, "phy80211");
  999. list_del_rcu(&wdev->list);
  1000. rdev->devlist_generation++;
  1001. cfg80211_mlme_purge_registrations(wdev);
  1002. #ifdef CONFIG_CFG80211_WEXT
  1003. kzfree(wdev->wext.keys);
  1004. #endif
  1005. }
  1006. /*
  1007. * synchronise (so that we won't find this netdev
  1008. * from other code any more) and then clear the list
  1009. * head so that the above code can safely check for
  1010. * !list_empty() to avoid double-cleanup.
  1011. */
  1012. synchronize_rcu();
  1013. INIT_LIST_HEAD(&wdev->list);
  1014. /*
  1015. * Ensure that all events have been processed and
  1016. * freed.
  1017. */
  1018. cfg80211_process_wdev_events(wdev);
  1019. if (WARN_ON(wdev->current_bss)) {
  1020. cfg80211_unhold_bss(wdev->current_bss);
  1021. cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
  1022. wdev->current_bss = NULL;
  1023. }
  1024. break;
  1025. case NETDEV_PRE_UP:
  1026. if (!(wdev->wiphy->interface_modes & BIT(wdev->iftype)))
  1027. return notifier_from_errno(-EOPNOTSUPP);
  1028. if (rfkill_blocked(rdev->rfkill))
  1029. return notifier_from_errno(-ERFKILL);
  1030. break;
  1031. default:
  1032. return NOTIFY_DONE;
  1033. }
  1034. wireless_nlevent_flush();
  1035. return NOTIFY_OK;
  1036. }
  1037. static struct notifier_block cfg80211_netdev_notifier = {
  1038. .notifier_call = cfg80211_netdev_notifier_call,
  1039. };
  1040. static void __net_exit cfg80211_pernet_exit(struct net *net)
  1041. {
  1042. struct cfg80211_registered_device *rdev;
  1043. rtnl_lock();
  1044. list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
  1045. if (net_eq(wiphy_net(&rdev->wiphy), net))
  1046. WARN_ON(cfg80211_switch_netns(rdev, &init_net));
  1047. }
  1048. rtnl_unlock();
  1049. }
  1050. static struct pernet_operations cfg80211_pernet_ops = {
  1051. .exit = cfg80211_pernet_exit,
  1052. };
  1053. static int __init cfg80211_init(void)
  1054. {
  1055. int err;
  1056. err = register_pernet_device(&cfg80211_pernet_ops);
  1057. if (err)
  1058. goto out_fail_pernet;
  1059. err = wiphy_sysfs_init();
  1060. if (err)
  1061. goto out_fail_sysfs;
  1062. err = register_netdevice_notifier(&cfg80211_netdev_notifier);
  1063. if (err)
  1064. goto out_fail_notifier;
  1065. err = nl80211_init();
  1066. if (err)
  1067. goto out_fail_nl80211;
  1068. ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
  1069. err = regulatory_init();
  1070. if (err)
  1071. goto out_fail_reg;
  1072. cfg80211_wq = alloc_ordered_workqueue("cfg80211", WQ_MEM_RECLAIM);
  1073. if (!cfg80211_wq) {
  1074. err = -ENOMEM;
  1075. goto out_fail_wq;
  1076. }
  1077. return 0;
  1078. out_fail_wq:
  1079. regulatory_exit();
  1080. out_fail_reg:
  1081. debugfs_remove(ieee80211_debugfs_dir);
  1082. nl80211_exit();
  1083. out_fail_nl80211:
  1084. unregister_netdevice_notifier(&cfg80211_netdev_notifier);
  1085. out_fail_notifier:
  1086. wiphy_sysfs_exit();
  1087. out_fail_sysfs:
  1088. unregister_pernet_device(&cfg80211_pernet_ops);
  1089. out_fail_pernet:
  1090. return err;
  1091. }
  1092. subsys_initcall(cfg80211_init);
  1093. static void __exit cfg80211_exit(void)
  1094. {
  1095. debugfs_remove(ieee80211_debugfs_dir);
  1096. nl80211_exit();
  1097. unregister_netdevice_notifier(&cfg80211_netdev_notifier);
  1098. wiphy_sysfs_exit();
  1099. regulatory_exit();
  1100. unregister_pernet_device(&cfg80211_pernet_ops);
  1101. destroy_workqueue(cfg80211_wq);
  1102. }
  1103. module_exit(cfg80211_exit);