core.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  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. !(wiphy->nan_supported_bands & BIT(NL80211_BAND_2GHZ)))))
  504. return -EINVAL;
  505. #ifndef CONFIG_WIRELESS_WDS
  506. if (WARN_ON(wiphy->interface_modes & BIT(NL80211_IFTYPE_WDS)))
  507. return -EINVAL;
  508. #endif
  509. /*
  510. * if a wiphy has unsupported modes for regulatory channel enforcement,
  511. * opt-out of enforcement checking
  512. */
  513. if (wiphy->interface_modes & ~(BIT(NL80211_IFTYPE_STATION) |
  514. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  515. BIT(NL80211_IFTYPE_AP) |
  516. BIT(NL80211_IFTYPE_P2P_GO) |
  517. BIT(NL80211_IFTYPE_ADHOC) |
  518. BIT(NL80211_IFTYPE_P2P_DEVICE) |
  519. BIT(NL80211_IFTYPE_NAN) |
  520. BIT(NL80211_IFTYPE_AP_VLAN) |
  521. BIT(NL80211_IFTYPE_MONITOR)))
  522. wiphy->regulatory_flags |= REGULATORY_IGNORE_STALE_KICKOFF;
  523. if (WARN_ON((wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) &&
  524. (wiphy->regulatory_flags &
  525. (REGULATORY_CUSTOM_REG |
  526. REGULATORY_STRICT_REG |
  527. REGULATORY_COUNTRY_IE_FOLLOW_POWER |
  528. REGULATORY_COUNTRY_IE_IGNORE))))
  529. return -EINVAL;
  530. if (WARN_ON(wiphy->coalesce &&
  531. (!wiphy->coalesce->n_rules ||
  532. !wiphy->coalesce->n_patterns) &&
  533. (!wiphy->coalesce->pattern_min_len ||
  534. wiphy->coalesce->pattern_min_len >
  535. wiphy->coalesce->pattern_max_len)))
  536. return -EINVAL;
  537. if (WARN_ON(wiphy->ap_sme_capa &&
  538. !(wiphy->flags & WIPHY_FLAG_HAVE_AP_SME)))
  539. return -EINVAL;
  540. if (WARN_ON(wiphy->addresses && !wiphy->n_addresses))
  541. return -EINVAL;
  542. if (WARN_ON(wiphy->addresses &&
  543. !is_zero_ether_addr(wiphy->perm_addr) &&
  544. memcmp(wiphy->perm_addr, wiphy->addresses[0].addr,
  545. ETH_ALEN)))
  546. return -EINVAL;
  547. if (WARN_ON(wiphy->max_acl_mac_addrs &&
  548. (!(wiphy->flags & WIPHY_FLAG_HAVE_AP_SME) ||
  549. !rdev->ops->set_mac_acl)))
  550. return -EINVAL;
  551. /* assure only valid behaviours are flagged by driver
  552. * hence subtract 2 as bit 0 is invalid.
  553. */
  554. if (WARN_ON(wiphy->bss_select_support &&
  555. (wiphy->bss_select_support & ~(BIT(__NL80211_BSS_SELECT_ATTR_AFTER_LAST) - 2))))
  556. return -EINVAL;
  557. if (wiphy->addresses)
  558. memcpy(wiphy->perm_addr, wiphy->addresses[0].addr, ETH_ALEN);
  559. /* sanity check ifmodes */
  560. WARN_ON(!ifmodes);
  561. ifmodes &= ((1 << NUM_NL80211_IFTYPES) - 1) & ~1;
  562. if (WARN_ON(ifmodes != wiphy->interface_modes))
  563. wiphy->interface_modes = ifmodes;
  564. res = wiphy_verify_combinations(wiphy);
  565. if (res)
  566. return res;
  567. /* sanity check supported bands/channels */
  568. for (band = 0; band < NUM_NL80211_BANDS; band++) {
  569. sband = wiphy->bands[band];
  570. if (!sband)
  571. continue;
  572. sband->band = band;
  573. if (WARN_ON(!sband->n_channels))
  574. return -EINVAL;
  575. /*
  576. * on 60GHz band, there are no legacy rates, so
  577. * n_bitrates is 0
  578. */
  579. if (WARN_ON(band != NL80211_BAND_60GHZ &&
  580. !sband->n_bitrates))
  581. return -EINVAL;
  582. /*
  583. * Since cfg80211_disable_40mhz_24ghz is global, we can
  584. * modify the sband's ht data even if the driver uses a
  585. * global structure for that.
  586. */
  587. if (cfg80211_disable_40mhz_24ghz &&
  588. band == NL80211_BAND_2GHZ &&
  589. sband->ht_cap.ht_supported) {
  590. sband->ht_cap.cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  591. sband->ht_cap.cap &= ~IEEE80211_HT_CAP_SGI_40;
  592. }
  593. /*
  594. * Since we use a u32 for rate bitmaps in
  595. * ieee80211_get_response_rate, we cannot
  596. * have more than 32 legacy rates.
  597. */
  598. if (WARN_ON(sband->n_bitrates > 32))
  599. return -EINVAL;
  600. for (i = 0; i < sband->n_channels; i++) {
  601. sband->channels[i].orig_flags =
  602. sband->channels[i].flags;
  603. sband->channels[i].orig_mag = INT_MAX;
  604. sband->channels[i].orig_mpwr =
  605. sband->channels[i].max_power;
  606. sband->channels[i].band = band;
  607. }
  608. have_band = true;
  609. }
  610. if (!have_band) {
  611. WARN_ON(1);
  612. return -EINVAL;
  613. }
  614. #ifdef CONFIG_PM
  615. if (WARN_ON(rdev->wiphy.wowlan && rdev->wiphy.wowlan->n_patterns &&
  616. (!rdev->wiphy.wowlan->pattern_min_len ||
  617. rdev->wiphy.wowlan->pattern_min_len >
  618. rdev->wiphy.wowlan->pattern_max_len)))
  619. return -EINVAL;
  620. #endif
  621. /* check and set up bitrates */
  622. ieee80211_set_bitrate_flags(wiphy);
  623. rdev->wiphy.features |= NL80211_FEATURE_SCAN_FLUSH;
  624. rtnl_lock();
  625. res = device_add(&rdev->wiphy.dev);
  626. if (res) {
  627. rtnl_unlock();
  628. return res;
  629. }
  630. /* set up regulatory info */
  631. wiphy_regulatory_register(wiphy);
  632. list_add_rcu(&rdev->list, &cfg80211_rdev_list);
  633. cfg80211_rdev_list_generation++;
  634. /* add to debugfs */
  635. rdev->wiphy.debugfsdir =
  636. debugfs_create_dir(wiphy_name(&rdev->wiphy),
  637. ieee80211_debugfs_dir);
  638. if (IS_ERR(rdev->wiphy.debugfsdir))
  639. rdev->wiphy.debugfsdir = NULL;
  640. cfg80211_debugfs_rdev_add(rdev);
  641. nl80211_notify_wiphy(rdev, NL80211_CMD_NEW_WIPHY);
  642. if (wiphy->regulatory_flags & REGULATORY_CUSTOM_REG) {
  643. struct regulatory_request request;
  644. request.wiphy_idx = get_wiphy_idx(wiphy);
  645. request.initiator = NL80211_REGDOM_SET_BY_DRIVER;
  646. request.alpha2[0] = '9';
  647. request.alpha2[1] = '9';
  648. nl80211_send_reg_change_event(&request);
  649. }
  650. /* Check that nobody globally advertises any capabilities they do not
  651. * advertise on all possible interface types.
  652. */
  653. if (wiphy->extended_capabilities_len &&
  654. wiphy->num_iftype_ext_capab &&
  655. wiphy->iftype_ext_capab) {
  656. u8 supported_on_all, j;
  657. const struct wiphy_iftype_ext_capab *capab;
  658. capab = wiphy->iftype_ext_capab;
  659. for (j = 0; j < wiphy->extended_capabilities_len; j++) {
  660. if (capab[0].extended_capabilities_len > j)
  661. supported_on_all =
  662. capab[0].extended_capabilities[j];
  663. else
  664. supported_on_all = 0x00;
  665. for (i = 1; i < wiphy->num_iftype_ext_capab; i++) {
  666. if (j >= capab[i].extended_capabilities_len) {
  667. supported_on_all = 0x00;
  668. break;
  669. }
  670. supported_on_all &=
  671. capab[i].extended_capabilities[j];
  672. }
  673. if (WARN_ON(wiphy->extended_capabilities[j] &
  674. ~supported_on_all))
  675. break;
  676. }
  677. }
  678. rdev->wiphy.registered = true;
  679. rtnl_unlock();
  680. res = rfkill_register(rdev->rfkill);
  681. if (res) {
  682. rfkill_destroy(rdev->rfkill);
  683. rdev->rfkill = NULL;
  684. wiphy_unregister(&rdev->wiphy);
  685. return res;
  686. }
  687. return 0;
  688. }
  689. EXPORT_SYMBOL(wiphy_register);
  690. void wiphy_rfkill_start_polling(struct wiphy *wiphy)
  691. {
  692. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  693. if (!rdev->ops->rfkill_poll)
  694. return;
  695. rdev->rfkill_ops.poll = cfg80211_rfkill_poll;
  696. rfkill_resume_polling(rdev->rfkill);
  697. }
  698. EXPORT_SYMBOL(wiphy_rfkill_start_polling);
  699. void wiphy_rfkill_stop_polling(struct wiphy *wiphy)
  700. {
  701. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  702. rfkill_pause_polling(rdev->rfkill);
  703. }
  704. EXPORT_SYMBOL(wiphy_rfkill_stop_polling);
  705. void wiphy_unregister(struct wiphy *wiphy)
  706. {
  707. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  708. wait_event(rdev->dev_wait, ({
  709. int __count;
  710. rtnl_lock();
  711. __count = rdev->opencount;
  712. rtnl_unlock();
  713. __count == 0; }));
  714. if (rdev->rfkill)
  715. rfkill_unregister(rdev->rfkill);
  716. rtnl_lock();
  717. nl80211_notify_wiphy(rdev, NL80211_CMD_DEL_WIPHY);
  718. rdev->wiphy.registered = false;
  719. WARN_ON(!list_empty(&rdev->wiphy.wdev_list));
  720. /*
  721. * First remove the hardware from everywhere, this makes
  722. * it impossible to find from userspace.
  723. */
  724. debugfs_remove_recursive(rdev->wiphy.debugfsdir);
  725. list_del_rcu(&rdev->list);
  726. synchronize_rcu();
  727. /*
  728. * If this device got a regulatory hint tell core its
  729. * free to listen now to a new shiny device regulatory hint
  730. */
  731. wiphy_regulatory_deregister(wiphy);
  732. cfg80211_rdev_list_generation++;
  733. device_del(&rdev->wiphy.dev);
  734. rtnl_unlock();
  735. flush_work(&rdev->scan_done_wk);
  736. cancel_work_sync(&rdev->conn_work);
  737. flush_work(&rdev->event_work);
  738. cancel_delayed_work_sync(&rdev->dfs_update_channels_wk);
  739. flush_work(&rdev->destroy_work);
  740. flush_work(&rdev->sched_scan_stop_wk);
  741. flush_work(&rdev->mlme_unreg_wk);
  742. #ifdef CONFIG_PM
  743. if (rdev->wiphy.wowlan_config && rdev->ops->set_wakeup)
  744. rdev_set_wakeup(rdev, false);
  745. #endif
  746. cfg80211_rdev_free_wowlan(rdev);
  747. cfg80211_rdev_free_coalesce(rdev);
  748. }
  749. EXPORT_SYMBOL(wiphy_unregister);
  750. void cfg80211_dev_free(struct cfg80211_registered_device *rdev)
  751. {
  752. struct cfg80211_internal_bss *scan, *tmp;
  753. struct cfg80211_beacon_registration *reg, *treg;
  754. rfkill_destroy(rdev->rfkill);
  755. list_for_each_entry_safe(reg, treg, &rdev->beacon_registrations, list) {
  756. list_del(&reg->list);
  757. kfree(reg);
  758. }
  759. list_for_each_entry_safe(scan, tmp, &rdev->bss_list, list)
  760. cfg80211_put_bss(&rdev->wiphy, &scan->pub);
  761. kfree(rdev);
  762. }
  763. void wiphy_free(struct wiphy *wiphy)
  764. {
  765. put_device(&wiphy->dev);
  766. }
  767. EXPORT_SYMBOL(wiphy_free);
  768. void wiphy_rfkill_set_hw_state(struct wiphy *wiphy, bool blocked)
  769. {
  770. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  771. if (rfkill_set_hw_state(rdev->rfkill, blocked))
  772. schedule_work(&rdev->rfkill_sync);
  773. }
  774. EXPORT_SYMBOL(wiphy_rfkill_set_hw_state);
  775. void cfg80211_unregister_wdev(struct wireless_dev *wdev)
  776. {
  777. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  778. ASSERT_RTNL();
  779. if (WARN_ON(wdev->netdev))
  780. return;
  781. nl80211_notify_iface(rdev, wdev, NL80211_CMD_DEL_INTERFACE);
  782. list_del_rcu(&wdev->list);
  783. rdev->devlist_generation++;
  784. switch (wdev->iftype) {
  785. case NL80211_IFTYPE_P2P_DEVICE:
  786. cfg80211_mlme_purge_registrations(wdev);
  787. cfg80211_stop_p2p_device(rdev, wdev);
  788. break;
  789. case NL80211_IFTYPE_NAN:
  790. cfg80211_stop_nan(rdev, wdev);
  791. break;
  792. default:
  793. WARN_ON_ONCE(1);
  794. break;
  795. }
  796. }
  797. EXPORT_SYMBOL(cfg80211_unregister_wdev);
  798. static const struct device_type wiphy_type = {
  799. .name = "wlan",
  800. };
  801. void cfg80211_update_iface_num(struct cfg80211_registered_device *rdev,
  802. enum nl80211_iftype iftype, int num)
  803. {
  804. ASSERT_RTNL();
  805. rdev->num_running_ifaces += num;
  806. if (iftype == NL80211_IFTYPE_MONITOR)
  807. rdev->num_running_monitor_ifaces += num;
  808. }
  809. void __cfg80211_leave(struct cfg80211_registered_device *rdev,
  810. struct wireless_dev *wdev)
  811. {
  812. struct net_device *dev = wdev->netdev;
  813. struct cfg80211_sched_scan_request *sched_scan_req;
  814. ASSERT_RTNL();
  815. ASSERT_WDEV_LOCK(wdev);
  816. switch (wdev->iftype) {
  817. case NL80211_IFTYPE_ADHOC:
  818. __cfg80211_leave_ibss(rdev, dev, true);
  819. break;
  820. case NL80211_IFTYPE_P2P_CLIENT:
  821. case NL80211_IFTYPE_STATION:
  822. sched_scan_req = rtnl_dereference(rdev->sched_scan_req);
  823. if (sched_scan_req && dev == sched_scan_req->dev)
  824. __cfg80211_stop_sched_scan(rdev, false);
  825. #ifdef CONFIG_CFG80211_WEXT
  826. kfree(wdev->wext.ie);
  827. wdev->wext.ie = NULL;
  828. wdev->wext.ie_len = 0;
  829. wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
  830. #endif
  831. cfg80211_disconnect(rdev, dev,
  832. WLAN_REASON_DEAUTH_LEAVING, true);
  833. break;
  834. case NL80211_IFTYPE_MESH_POINT:
  835. __cfg80211_leave_mesh(rdev, dev);
  836. break;
  837. case NL80211_IFTYPE_AP:
  838. case NL80211_IFTYPE_P2P_GO:
  839. __cfg80211_stop_ap(rdev, dev, true);
  840. break;
  841. case NL80211_IFTYPE_OCB:
  842. __cfg80211_leave_ocb(rdev, dev);
  843. break;
  844. case NL80211_IFTYPE_WDS:
  845. /* must be handled by mac80211/driver, has no APIs */
  846. break;
  847. case NL80211_IFTYPE_P2P_DEVICE:
  848. case NL80211_IFTYPE_NAN:
  849. /* cannot happen, has no netdev */
  850. break;
  851. case NL80211_IFTYPE_AP_VLAN:
  852. case NL80211_IFTYPE_MONITOR:
  853. /* nothing to do */
  854. break;
  855. case NL80211_IFTYPE_UNSPECIFIED:
  856. case NUM_NL80211_IFTYPES:
  857. /* invalid */
  858. break;
  859. }
  860. }
  861. void cfg80211_leave(struct cfg80211_registered_device *rdev,
  862. struct wireless_dev *wdev)
  863. {
  864. wdev_lock(wdev);
  865. __cfg80211_leave(rdev, wdev);
  866. wdev_unlock(wdev);
  867. }
  868. void cfg80211_stop_iface(struct wiphy *wiphy, struct wireless_dev *wdev,
  869. gfp_t gfp)
  870. {
  871. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  872. struct cfg80211_event *ev;
  873. unsigned long flags;
  874. trace_cfg80211_stop_iface(wiphy, wdev);
  875. ev = kzalloc(sizeof(*ev), gfp);
  876. if (!ev)
  877. return;
  878. ev->type = EVENT_STOPPED;
  879. spin_lock_irqsave(&wdev->event_lock, flags);
  880. list_add_tail(&ev->list, &wdev->event_list);
  881. spin_unlock_irqrestore(&wdev->event_lock, flags);
  882. queue_work(cfg80211_wq, &rdev->event_work);
  883. }
  884. EXPORT_SYMBOL(cfg80211_stop_iface);
  885. static int cfg80211_netdev_notifier_call(struct notifier_block *nb,
  886. unsigned long state, void *ptr)
  887. {
  888. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  889. struct wireless_dev *wdev = dev->ieee80211_ptr;
  890. struct cfg80211_registered_device *rdev;
  891. struct cfg80211_sched_scan_request *sched_scan_req;
  892. if (!wdev)
  893. return NOTIFY_DONE;
  894. rdev = wiphy_to_rdev(wdev->wiphy);
  895. WARN_ON(wdev->iftype == NL80211_IFTYPE_UNSPECIFIED);
  896. switch (state) {
  897. case NETDEV_POST_INIT:
  898. SET_NETDEV_DEVTYPE(dev, &wiphy_type);
  899. break;
  900. case NETDEV_REGISTER:
  901. /*
  902. * NB: cannot take rdev->mtx here because this may be
  903. * called within code protected by it when interfaces
  904. * are added with nl80211.
  905. */
  906. mutex_init(&wdev->mtx);
  907. INIT_LIST_HEAD(&wdev->event_list);
  908. spin_lock_init(&wdev->event_lock);
  909. INIT_LIST_HEAD(&wdev->mgmt_registrations);
  910. spin_lock_init(&wdev->mgmt_registrations_lock);
  911. wdev->identifier = ++rdev->wdev_id;
  912. list_add_rcu(&wdev->list, &rdev->wiphy.wdev_list);
  913. rdev->devlist_generation++;
  914. /* can only change netns with wiphy */
  915. dev->features |= NETIF_F_NETNS_LOCAL;
  916. if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj,
  917. "phy80211")) {
  918. pr_err("failed to add phy80211 symlink to netdev!\n");
  919. }
  920. wdev->netdev = dev;
  921. #ifdef CONFIG_CFG80211_WEXT
  922. wdev->wext.default_key = -1;
  923. wdev->wext.default_mgmt_key = -1;
  924. wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
  925. #endif
  926. if (wdev->wiphy->flags & WIPHY_FLAG_PS_ON_BY_DEFAULT)
  927. wdev->ps = true;
  928. else
  929. wdev->ps = false;
  930. /* allow mac80211 to determine the timeout */
  931. wdev->ps_timeout = -1;
  932. if ((wdev->iftype == NL80211_IFTYPE_STATION ||
  933. wdev->iftype == NL80211_IFTYPE_P2P_CLIENT ||
  934. wdev->iftype == NL80211_IFTYPE_ADHOC) && !wdev->use_4addr)
  935. dev->priv_flags |= IFF_DONT_BRIDGE;
  936. INIT_WORK(&wdev->disconnect_wk, cfg80211_autodisconnect_wk);
  937. nl80211_notify_iface(rdev, wdev, NL80211_CMD_NEW_INTERFACE);
  938. break;
  939. case NETDEV_GOING_DOWN:
  940. cfg80211_leave(rdev, wdev);
  941. break;
  942. case NETDEV_DOWN:
  943. cfg80211_update_iface_num(rdev, wdev->iftype, -1);
  944. if (rdev->scan_req && rdev->scan_req->wdev == wdev) {
  945. if (WARN_ON(!rdev->scan_req->notified))
  946. rdev->scan_req->info.aborted = true;
  947. ___cfg80211_scan_done(rdev, false);
  948. }
  949. sched_scan_req = rtnl_dereference(rdev->sched_scan_req);
  950. if (WARN_ON(sched_scan_req &&
  951. sched_scan_req->dev == wdev->netdev)) {
  952. __cfg80211_stop_sched_scan(rdev, false);
  953. }
  954. rdev->opencount--;
  955. wake_up(&rdev->dev_wait);
  956. break;
  957. case NETDEV_UP:
  958. cfg80211_update_iface_num(rdev, wdev->iftype, 1);
  959. wdev_lock(wdev);
  960. switch (wdev->iftype) {
  961. #ifdef CONFIG_CFG80211_WEXT
  962. case NL80211_IFTYPE_ADHOC:
  963. cfg80211_ibss_wext_join(rdev, wdev);
  964. break;
  965. case NL80211_IFTYPE_STATION:
  966. cfg80211_mgd_wext_connect(rdev, wdev);
  967. break;
  968. #endif
  969. #ifdef CONFIG_MAC80211_MESH
  970. case NL80211_IFTYPE_MESH_POINT:
  971. {
  972. /* backward compat code... */
  973. struct mesh_setup setup;
  974. memcpy(&setup, &default_mesh_setup,
  975. sizeof(setup));
  976. /* back compat only needed for mesh_id */
  977. setup.mesh_id = wdev->ssid;
  978. setup.mesh_id_len = wdev->mesh_id_up_len;
  979. if (wdev->mesh_id_up_len)
  980. __cfg80211_join_mesh(rdev, dev,
  981. &setup,
  982. &default_mesh_config);
  983. break;
  984. }
  985. #endif
  986. default:
  987. break;
  988. }
  989. wdev_unlock(wdev);
  990. rdev->opencount++;
  991. /*
  992. * Configure power management to the driver here so that its
  993. * correctly set also after interface type changes etc.
  994. */
  995. if ((wdev->iftype == NL80211_IFTYPE_STATION ||
  996. wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) &&
  997. rdev->ops->set_power_mgmt)
  998. if (rdev_set_power_mgmt(rdev, dev, wdev->ps,
  999. wdev->ps_timeout)) {
  1000. /* assume this means it's off */
  1001. wdev->ps = false;
  1002. }
  1003. break;
  1004. case NETDEV_UNREGISTER:
  1005. /*
  1006. * It is possible to get NETDEV_UNREGISTER
  1007. * multiple times. To detect that, check
  1008. * that the interface is still on the list
  1009. * of registered interfaces, and only then
  1010. * remove and clean it up.
  1011. */
  1012. if (!list_empty(&wdev->list)) {
  1013. nl80211_notify_iface(rdev, wdev,
  1014. NL80211_CMD_DEL_INTERFACE);
  1015. sysfs_remove_link(&dev->dev.kobj, "phy80211");
  1016. list_del_rcu(&wdev->list);
  1017. rdev->devlist_generation++;
  1018. cfg80211_mlme_purge_registrations(wdev);
  1019. #ifdef CONFIG_CFG80211_WEXT
  1020. kzfree(wdev->wext.keys);
  1021. #endif
  1022. flush_work(&wdev->disconnect_wk);
  1023. }
  1024. /*
  1025. * synchronise (so that we won't find this netdev
  1026. * from other code any more) and then clear the list
  1027. * head so that the above code can safely check for
  1028. * !list_empty() to avoid double-cleanup.
  1029. */
  1030. synchronize_rcu();
  1031. INIT_LIST_HEAD(&wdev->list);
  1032. /*
  1033. * Ensure that all events have been processed and
  1034. * freed.
  1035. */
  1036. cfg80211_process_wdev_events(wdev);
  1037. if (WARN_ON(wdev->current_bss)) {
  1038. cfg80211_unhold_bss(wdev->current_bss);
  1039. cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
  1040. wdev->current_bss = NULL;
  1041. }
  1042. break;
  1043. case NETDEV_PRE_UP:
  1044. if (!(wdev->wiphy->interface_modes & BIT(wdev->iftype)))
  1045. return notifier_from_errno(-EOPNOTSUPP);
  1046. if (rfkill_blocked(rdev->rfkill))
  1047. return notifier_from_errno(-ERFKILL);
  1048. break;
  1049. default:
  1050. return NOTIFY_DONE;
  1051. }
  1052. wireless_nlevent_flush();
  1053. return NOTIFY_OK;
  1054. }
  1055. static struct notifier_block cfg80211_netdev_notifier = {
  1056. .notifier_call = cfg80211_netdev_notifier_call,
  1057. };
  1058. static void __net_exit cfg80211_pernet_exit(struct net *net)
  1059. {
  1060. struct cfg80211_registered_device *rdev;
  1061. rtnl_lock();
  1062. list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
  1063. if (net_eq(wiphy_net(&rdev->wiphy), net))
  1064. WARN_ON(cfg80211_switch_netns(rdev, &init_net));
  1065. }
  1066. rtnl_unlock();
  1067. }
  1068. static struct pernet_operations cfg80211_pernet_ops = {
  1069. .exit = cfg80211_pernet_exit,
  1070. };
  1071. static int __init cfg80211_init(void)
  1072. {
  1073. int err;
  1074. err = register_pernet_device(&cfg80211_pernet_ops);
  1075. if (err)
  1076. goto out_fail_pernet;
  1077. err = wiphy_sysfs_init();
  1078. if (err)
  1079. goto out_fail_sysfs;
  1080. err = register_netdevice_notifier(&cfg80211_netdev_notifier);
  1081. if (err)
  1082. goto out_fail_notifier;
  1083. err = nl80211_init();
  1084. if (err)
  1085. goto out_fail_nl80211;
  1086. ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
  1087. err = regulatory_init();
  1088. if (err)
  1089. goto out_fail_reg;
  1090. cfg80211_wq = alloc_ordered_workqueue("cfg80211", WQ_MEM_RECLAIM);
  1091. if (!cfg80211_wq) {
  1092. err = -ENOMEM;
  1093. goto out_fail_wq;
  1094. }
  1095. return 0;
  1096. out_fail_wq:
  1097. regulatory_exit();
  1098. out_fail_reg:
  1099. debugfs_remove(ieee80211_debugfs_dir);
  1100. nl80211_exit();
  1101. out_fail_nl80211:
  1102. unregister_netdevice_notifier(&cfg80211_netdev_notifier);
  1103. out_fail_notifier:
  1104. wiphy_sysfs_exit();
  1105. out_fail_sysfs:
  1106. unregister_pernet_device(&cfg80211_pernet_ops);
  1107. out_fail_pernet:
  1108. return err;
  1109. }
  1110. subsys_initcall(cfg80211_init);
  1111. static void __exit cfg80211_exit(void)
  1112. {
  1113. debugfs_remove(ieee80211_debugfs_dir);
  1114. nl80211_exit();
  1115. unregister_netdevice_notifier(&cfg80211_netdev_notifier);
  1116. wiphy_sysfs_exit();
  1117. regulatory_exit();
  1118. unregister_pernet_device(&cfg80211_pernet_ops);
  1119. destroy_workqueue(cfg80211_wq);
  1120. }
  1121. module_exit(cfg80211_exit);