core.c 36 KB

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