core.c 34 KB

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