core.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413
  1. /*
  2. * This is the linux wireless configuration interface.
  3. *
  4. * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
  5. * Copyright 2013-2014 Intel Mobile Communications GmbH
  6. * Copyright 2015 Intel Deutschland GmbH
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/if.h>
  10. #include <linux/module.h>
  11. #include <linux/err.h>
  12. #include <linux/list.h>
  13. #include <linux/slab.h>
  14. #include <linux/nl80211.h>
  15. #include <linux/debugfs.h>
  16. #include <linux/notifier.h>
  17. #include <linux/device.h>
  18. #include <linux/etherdevice.h>
  19. #include <linux/rtnetlink.h>
  20. #include <linux/sched.h>
  21. #include <net/genetlink.h>
  22. #include <net/cfg80211.h>
  23. #include "nl80211.h"
  24. #include "core.h"
  25. #include "sysfs.h"
  26. #include "debugfs.h"
  27. #include "wext-compat.h"
  28. #include "rdev-ops.h"
  29. /* name for sysfs, %d is appended */
  30. #define PHY_NAME "phy"
  31. MODULE_AUTHOR("Johannes Berg");
  32. MODULE_LICENSE("GPL");
  33. MODULE_DESCRIPTION("wireless configuration support");
  34. MODULE_ALIAS_GENL_FAMILY(NL80211_GENL_NAME);
  35. /* RCU-protected (and RTNL for writers) */
  36. LIST_HEAD(cfg80211_rdev_list);
  37. int cfg80211_rdev_list_generation;
  38. /* for debugfs */
  39. static struct dentry *ieee80211_debugfs_dir;
  40. /* for the cleanup, scan and event works */
  41. struct workqueue_struct *cfg80211_wq;
  42. static bool cfg80211_disable_40mhz_24ghz;
  43. module_param(cfg80211_disable_40mhz_24ghz, bool, 0644);
  44. MODULE_PARM_DESC(cfg80211_disable_40mhz_24ghz,
  45. "Disable 40MHz support in the 2.4GHz band");
  46. struct cfg80211_registered_device *cfg80211_rdev_by_wiphy_idx(int wiphy_idx)
  47. {
  48. struct cfg80211_registered_device *result = NULL, *rdev;
  49. ASSERT_RTNL();
  50. list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
  51. if (rdev->wiphy_idx == wiphy_idx) {
  52. result = rdev;
  53. break;
  54. }
  55. }
  56. return result;
  57. }
  58. int get_wiphy_idx(struct wiphy *wiphy)
  59. {
  60. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  61. return rdev->wiphy_idx;
  62. }
  63. struct wiphy *wiphy_idx_to_wiphy(int wiphy_idx)
  64. {
  65. struct cfg80211_registered_device *rdev;
  66. ASSERT_RTNL();
  67. rdev = cfg80211_rdev_by_wiphy_idx(wiphy_idx);
  68. if (!rdev)
  69. return NULL;
  70. return &rdev->wiphy;
  71. }
  72. static int cfg80211_dev_check_name(struct cfg80211_registered_device *rdev,
  73. const char *newname)
  74. {
  75. struct cfg80211_registered_device *rdev2;
  76. int wiphy_idx, taken = -1, digits;
  77. ASSERT_RTNL();
  78. if (strlen(newname) > NL80211_WIPHY_NAME_MAXLEN)
  79. return -EINVAL;
  80. /* prohibit calling the thing phy%d when %d is not its number */
  81. sscanf(newname, PHY_NAME "%d%n", &wiphy_idx, &taken);
  82. if (taken == strlen(newname) && wiphy_idx != rdev->wiphy_idx) {
  83. /* count number of places needed to print wiphy_idx */
  84. digits = 1;
  85. while (wiphy_idx /= 10)
  86. digits++;
  87. /*
  88. * deny the name if it is phy<idx> where <idx> is printed
  89. * without leading zeroes. taken == strlen(newname) here
  90. */
  91. if (taken == strlen(PHY_NAME) + digits)
  92. return -EINVAL;
  93. }
  94. /* Ensure another device does not already have this name. */
  95. list_for_each_entry(rdev2, &cfg80211_rdev_list, list)
  96. if (strcmp(newname, wiphy_name(&rdev2->wiphy)) == 0)
  97. return -EINVAL;
  98. return 0;
  99. }
  100. int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
  101. char *newname)
  102. {
  103. int result;
  104. ASSERT_RTNL();
  105. /* Ignore nop renames */
  106. if (strcmp(newname, wiphy_name(&rdev->wiphy)) == 0)
  107. return 0;
  108. result = cfg80211_dev_check_name(rdev, newname);
  109. if (result < 0)
  110. return result;
  111. result = device_rename(&rdev->wiphy.dev, newname);
  112. if (result)
  113. return result;
  114. if (rdev->wiphy.debugfsdir &&
  115. !debugfs_rename(rdev->wiphy.debugfsdir->d_parent,
  116. rdev->wiphy.debugfsdir,
  117. rdev->wiphy.debugfsdir->d_parent,
  118. newname))
  119. pr_err("failed to rename debugfs dir to %s!\n", newname);
  120. nl80211_notify_wiphy(rdev, NL80211_CMD_NEW_WIPHY);
  121. return 0;
  122. }
  123. int cfg80211_switch_netns(struct cfg80211_registered_device *rdev,
  124. struct net *net)
  125. {
  126. struct wireless_dev *wdev;
  127. int err = 0;
  128. if (!(rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK))
  129. return -EOPNOTSUPP;
  130. list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
  131. if (!wdev->netdev)
  132. continue;
  133. wdev->netdev->features &= ~NETIF_F_NETNS_LOCAL;
  134. err = dev_change_net_namespace(wdev->netdev, net, "wlan%d");
  135. if (err)
  136. break;
  137. wdev->netdev->features |= NETIF_F_NETNS_LOCAL;
  138. }
  139. if (err) {
  140. /* failed -- clean up to old netns */
  141. net = wiphy_net(&rdev->wiphy);
  142. list_for_each_entry_continue_reverse(wdev,
  143. &rdev->wiphy.wdev_list,
  144. list) {
  145. if (!wdev->netdev)
  146. continue;
  147. wdev->netdev->features &= ~NETIF_F_NETNS_LOCAL;
  148. err = dev_change_net_namespace(wdev->netdev, net,
  149. "wlan%d");
  150. WARN_ON(err);
  151. wdev->netdev->features |= NETIF_F_NETNS_LOCAL;
  152. }
  153. return err;
  154. }
  155. wiphy_net_set(&rdev->wiphy, net);
  156. err = device_rename(&rdev->wiphy.dev, dev_name(&rdev->wiphy.dev));
  157. WARN_ON(err);
  158. return 0;
  159. }
  160. static void cfg80211_rfkill_poll(struct rfkill *rfkill, void *data)
  161. {
  162. struct cfg80211_registered_device *rdev = data;
  163. rdev_rfkill_poll(rdev);
  164. }
  165. void cfg80211_stop_p2p_device(struct cfg80211_registered_device *rdev,
  166. struct wireless_dev *wdev)
  167. {
  168. ASSERT_RTNL();
  169. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_P2P_DEVICE))
  170. return;
  171. if (!wdev_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. kfree(rdev);
  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. sband = wiphy->bands[band];
  599. if (!sband)
  600. continue;
  601. sband->band = band;
  602. if (WARN_ON(!sband->n_channels))
  603. return -EINVAL;
  604. /*
  605. * on 60GHz band, there are no legacy rates, so
  606. * n_bitrates is 0
  607. */
  608. if (WARN_ON(band != NL80211_BAND_60GHZ &&
  609. !sband->n_bitrates))
  610. return -EINVAL;
  611. /*
  612. * Since cfg80211_disable_40mhz_24ghz is global, we can
  613. * modify the sband's ht data even if the driver uses a
  614. * global structure for that.
  615. */
  616. if (cfg80211_disable_40mhz_24ghz &&
  617. band == NL80211_BAND_2GHZ &&
  618. sband->ht_cap.ht_supported) {
  619. sband->ht_cap.cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  620. sband->ht_cap.cap &= ~IEEE80211_HT_CAP_SGI_40;
  621. }
  622. /*
  623. * Since we use a u32 for rate bitmaps in
  624. * ieee80211_get_response_rate, we cannot
  625. * have more than 32 legacy rates.
  626. */
  627. if (WARN_ON(sband->n_bitrates > 32))
  628. return -EINVAL;
  629. for (i = 0; i < sband->n_channels; i++) {
  630. sband->channels[i].orig_flags =
  631. sband->channels[i].flags;
  632. sband->channels[i].orig_mag = INT_MAX;
  633. sband->channels[i].orig_mpwr =
  634. sband->channels[i].max_power;
  635. sband->channels[i].band = band;
  636. }
  637. have_band = true;
  638. }
  639. if (!have_band) {
  640. WARN_ON(1);
  641. return -EINVAL;
  642. }
  643. #ifdef CONFIG_PM
  644. if (WARN_ON(rdev->wiphy.wowlan && rdev->wiphy.wowlan->n_patterns &&
  645. (!rdev->wiphy.wowlan->pattern_min_len ||
  646. rdev->wiphy.wowlan->pattern_min_len >
  647. rdev->wiphy.wowlan->pattern_max_len)))
  648. return -EINVAL;
  649. #endif
  650. /* check and set up bitrates */
  651. ieee80211_set_bitrate_flags(wiphy);
  652. rdev->wiphy.features |= NL80211_FEATURE_SCAN_FLUSH;
  653. rtnl_lock();
  654. res = device_add(&rdev->wiphy.dev);
  655. if (res) {
  656. rtnl_unlock();
  657. return res;
  658. }
  659. /* set up regulatory info */
  660. wiphy_regulatory_register(wiphy);
  661. list_add_rcu(&rdev->list, &cfg80211_rdev_list);
  662. cfg80211_rdev_list_generation++;
  663. /* add to debugfs */
  664. rdev->wiphy.debugfsdir =
  665. debugfs_create_dir(wiphy_name(&rdev->wiphy),
  666. ieee80211_debugfs_dir);
  667. if (IS_ERR(rdev->wiphy.debugfsdir))
  668. rdev->wiphy.debugfsdir = NULL;
  669. cfg80211_debugfs_rdev_add(rdev);
  670. nl80211_notify_wiphy(rdev, NL80211_CMD_NEW_WIPHY);
  671. if (wiphy->regulatory_flags & REGULATORY_CUSTOM_REG) {
  672. struct regulatory_request request;
  673. request.wiphy_idx = get_wiphy_idx(wiphy);
  674. request.initiator = NL80211_REGDOM_SET_BY_DRIVER;
  675. request.alpha2[0] = '9';
  676. request.alpha2[1] = '9';
  677. nl80211_send_reg_change_event(&request);
  678. }
  679. /* Check that nobody globally advertises any capabilities they do not
  680. * advertise on all possible interface types.
  681. */
  682. if (wiphy->extended_capabilities_len &&
  683. wiphy->num_iftype_ext_capab &&
  684. wiphy->iftype_ext_capab) {
  685. u8 supported_on_all, j;
  686. const struct wiphy_iftype_ext_capab *capab;
  687. capab = wiphy->iftype_ext_capab;
  688. for (j = 0; j < wiphy->extended_capabilities_len; j++) {
  689. if (capab[0].extended_capabilities_len > j)
  690. supported_on_all =
  691. capab[0].extended_capabilities[j];
  692. else
  693. supported_on_all = 0x00;
  694. for (i = 1; i < wiphy->num_iftype_ext_capab; i++) {
  695. if (j >= capab[i].extended_capabilities_len) {
  696. supported_on_all = 0x00;
  697. break;
  698. }
  699. supported_on_all &=
  700. capab[i].extended_capabilities[j];
  701. }
  702. if (WARN_ON(wiphy->extended_capabilities[j] &
  703. ~supported_on_all))
  704. break;
  705. }
  706. }
  707. rdev->wiphy.registered = true;
  708. rtnl_unlock();
  709. res = rfkill_register(rdev->rfkill);
  710. if (res) {
  711. rfkill_destroy(rdev->rfkill);
  712. rdev->rfkill = NULL;
  713. wiphy_unregister(&rdev->wiphy);
  714. return res;
  715. }
  716. return 0;
  717. }
  718. EXPORT_SYMBOL(wiphy_register);
  719. void wiphy_rfkill_start_polling(struct wiphy *wiphy)
  720. {
  721. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  722. if (!rdev->ops->rfkill_poll)
  723. return;
  724. rdev->rfkill_ops.poll = cfg80211_rfkill_poll;
  725. rfkill_resume_polling(rdev->rfkill);
  726. }
  727. EXPORT_SYMBOL(wiphy_rfkill_start_polling);
  728. void wiphy_rfkill_stop_polling(struct wiphy *wiphy)
  729. {
  730. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  731. rfkill_pause_polling(rdev->rfkill);
  732. }
  733. EXPORT_SYMBOL(wiphy_rfkill_stop_polling);
  734. void wiphy_unregister(struct wiphy *wiphy)
  735. {
  736. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  737. wait_event(rdev->dev_wait, ({
  738. int __count;
  739. rtnl_lock();
  740. __count = rdev->opencount;
  741. rtnl_unlock();
  742. __count == 0; }));
  743. if (rdev->rfkill)
  744. rfkill_unregister(rdev->rfkill);
  745. rtnl_lock();
  746. nl80211_notify_wiphy(rdev, NL80211_CMD_DEL_WIPHY);
  747. rdev->wiphy.registered = false;
  748. WARN_ON(!list_empty(&rdev->wiphy.wdev_list));
  749. /*
  750. * First remove the hardware from everywhere, this makes
  751. * it impossible to find from userspace.
  752. */
  753. debugfs_remove_recursive(rdev->wiphy.debugfsdir);
  754. list_del_rcu(&rdev->list);
  755. synchronize_rcu();
  756. /*
  757. * If this device got a regulatory hint tell core its
  758. * free to listen now to a new shiny device regulatory hint
  759. */
  760. wiphy_regulatory_deregister(wiphy);
  761. cfg80211_rdev_list_generation++;
  762. device_del(&rdev->wiphy.dev);
  763. rtnl_unlock();
  764. flush_work(&rdev->scan_done_wk);
  765. cancel_work_sync(&rdev->conn_work);
  766. flush_work(&rdev->event_work);
  767. cancel_delayed_work_sync(&rdev->dfs_update_channels_wk);
  768. flush_work(&rdev->destroy_work);
  769. flush_work(&rdev->sched_scan_stop_wk);
  770. flush_work(&rdev->mlme_unreg_wk);
  771. flush_work(&rdev->propagate_radar_detect_wk);
  772. flush_work(&rdev->propagate_cac_done_wk);
  773. #ifdef CONFIG_PM
  774. if (rdev->wiphy.wowlan_config && rdev->ops->set_wakeup)
  775. rdev_set_wakeup(rdev, false);
  776. #endif
  777. cfg80211_rdev_free_wowlan(rdev);
  778. cfg80211_rdev_free_coalesce(rdev);
  779. }
  780. EXPORT_SYMBOL(wiphy_unregister);
  781. void cfg80211_dev_free(struct cfg80211_registered_device *rdev)
  782. {
  783. struct cfg80211_internal_bss *scan, *tmp;
  784. struct cfg80211_beacon_registration *reg, *treg;
  785. rfkill_destroy(rdev->rfkill);
  786. list_for_each_entry_safe(reg, treg, &rdev->beacon_registrations, list) {
  787. list_del(&reg->list);
  788. kfree(reg);
  789. }
  790. list_for_each_entry_safe(scan, tmp, &rdev->bss_list, list)
  791. cfg80211_put_bss(&rdev->wiphy, &scan->pub);
  792. kfree(rdev);
  793. }
  794. void wiphy_free(struct wiphy *wiphy)
  795. {
  796. put_device(&wiphy->dev);
  797. }
  798. EXPORT_SYMBOL(wiphy_free);
  799. void wiphy_rfkill_set_hw_state(struct wiphy *wiphy, bool blocked)
  800. {
  801. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  802. if (rfkill_set_hw_state(rdev->rfkill, blocked))
  803. schedule_work(&rdev->rfkill_sync);
  804. }
  805. EXPORT_SYMBOL(wiphy_rfkill_set_hw_state);
  806. void cfg80211_cqm_config_free(struct wireless_dev *wdev)
  807. {
  808. kfree(wdev->cqm_config);
  809. wdev->cqm_config = NULL;
  810. }
  811. void cfg80211_unregister_wdev(struct wireless_dev *wdev)
  812. {
  813. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  814. ASSERT_RTNL();
  815. if (WARN_ON(wdev->netdev))
  816. return;
  817. nl80211_notify_iface(rdev, wdev, NL80211_CMD_DEL_INTERFACE);
  818. list_del_rcu(&wdev->list);
  819. synchronize_rcu();
  820. rdev->devlist_generation++;
  821. switch (wdev->iftype) {
  822. case NL80211_IFTYPE_P2P_DEVICE:
  823. cfg80211_mlme_purge_registrations(wdev);
  824. cfg80211_stop_p2p_device(rdev, wdev);
  825. break;
  826. case NL80211_IFTYPE_NAN:
  827. cfg80211_stop_nan(rdev, wdev);
  828. break;
  829. default:
  830. WARN_ON_ONCE(1);
  831. break;
  832. }
  833. cfg80211_cqm_config_free(wdev);
  834. }
  835. EXPORT_SYMBOL(cfg80211_unregister_wdev);
  836. static const struct device_type wiphy_type = {
  837. .name = "wlan",
  838. };
  839. void cfg80211_update_iface_num(struct cfg80211_registered_device *rdev,
  840. enum nl80211_iftype iftype, int num)
  841. {
  842. ASSERT_RTNL();
  843. rdev->num_running_ifaces += num;
  844. if (iftype == NL80211_IFTYPE_MONITOR)
  845. rdev->num_running_monitor_ifaces += num;
  846. }
  847. void __cfg80211_leave(struct cfg80211_registered_device *rdev,
  848. struct wireless_dev *wdev)
  849. {
  850. struct net_device *dev = wdev->netdev;
  851. struct cfg80211_sched_scan_request *pos, *tmp;
  852. ASSERT_RTNL();
  853. ASSERT_WDEV_LOCK(wdev);
  854. switch (wdev->iftype) {
  855. case NL80211_IFTYPE_ADHOC:
  856. __cfg80211_leave_ibss(rdev, dev, true);
  857. break;
  858. case NL80211_IFTYPE_P2P_CLIENT:
  859. case NL80211_IFTYPE_STATION:
  860. list_for_each_entry_safe(pos, tmp, &rdev->sched_scan_req_list,
  861. list) {
  862. if (dev == pos->dev)
  863. cfg80211_stop_sched_scan_req(rdev, pos, false);
  864. }
  865. #ifdef CONFIG_CFG80211_WEXT
  866. kfree(wdev->wext.ie);
  867. wdev->wext.ie = NULL;
  868. wdev->wext.ie_len = 0;
  869. wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
  870. #endif
  871. cfg80211_disconnect(rdev, dev,
  872. WLAN_REASON_DEAUTH_LEAVING, true);
  873. break;
  874. case NL80211_IFTYPE_MESH_POINT:
  875. __cfg80211_leave_mesh(rdev, dev);
  876. break;
  877. case NL80211_IFTYPE_AP:
  878. case NL80211_IFTYPE_P2P_GO:
  879. __cfg80211_stop_ap(rdev, dev, true);
  880. break;
  881. case NL80211_IFTYPE_OCB:
  882. __cfg80211_leave_ocb(rdev, dev);
  883. break;
  884. case NL80211_IFTYPE_WDS:
  885. /* must be handled by mac80211/driver, has no APIs */
  886. break;
  887. case NL80211_IFTYPE_P2P_DEVICE:
  888. case NL80211_IFTYPE_NAN:
  889. /* cannot happen, has no netdev */
  890. break;
  891. case NL80211_IFTYPE_AP_VLAN:
  892. case NL80211_IFTYPE_MONITOR:
  893. /* nothing to do */
  894. break;
  895. case NL80211_IFTYPE_UNSPECIFIED:
  896. case NUM_NL80211_IFTYPES:
  897. /* invalid */
  898. break;
  899. }
  900. }
  901. void cfg80211_leave(struct cfg80211_registered_device *rdev,
  902. struct wireless_dev *wdev)
  903. {
  904. wdev_lock(wdev);
  905. __cfg80211_leave(rdev, wdev);
  906. wdev_unlock(wdev);
  907. }
  908. void cfg80211_stop_iface(struct wiphy *wiphy, struct wireless_dev *wdev,
  909. gfp_t gfp)
  910. {
  911. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  912. struct cfg80211_event *ev;
  913. unsigned long flags;
  914. trace_cfg80211_stop_iface(wiphy, wdev);
  915. ev = kzalloc(sizeof(*ev), gfp);
  916. if (!ev)
  917. return;
  918. ev->type = EVENT_STOPPED;
  919. spin_lock_irqsave(&wdev->event_lock, flags);
  920. list_add_tail(&ev->list, &wdev->event_list);
  921. spin_unlock_irqrestore(&wdev->event_lock, flags);
  922. queue_work(cfg80211_wq, &rdev->event_work);
  923. }
  924. EXPORT_SYMBOL(cfg80211_stop_iface);
  925. static int cfg80211_netdev_notifier_call(struct notifier_block *nb,
  926. unsigned long state, void *ptr)
  927. {
  928. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  929. struct wireless_dev *wdev = dev->ieee80211_ptr;
  930. struct cfg80211_registered_device *rdev;
  931. struct cfg80211_sched_scan_request *pos, *tmp;
  932. if (!wdev)
  933. return NOTIFY_DONE;
  934. rdev = wiphy_to_rdev(wdev->wiphy);
  935. WARN_ON(wdev->iftype == NL80211_IFTYPE_UNSPECIFIED);
  936. switch (state) {
  937. case NETDEV_POST_INIT:
  938. SET_NETDEV_DEVTYPE(dev, &wiphy_type);
  939. break;
  940. case NETDEV_REGISTER:
  941. /*
  942. * NB: cannot take rdev->mtx here because this may be
  943. * called within code protected by it when interfaces
  944. * are added with nl80211.
  945. */
  946. mutex_init(&wdev->mtx);
  947. INIT_LIST_HEAD(&wdev->event_list);
  948. spin_lock_init(&wdev->event_lock);
  949. INIT_LIST_HEAD(&wdev->mgmt_registrations);
  950. spin_lock_init(&wdev->mgmt_registrations_lock);
  951. /*
  952. * We get here also when the interface changes network namespaces,
  953. * as it's registered into the new one, but we don't want it to
  954. * change ID in that case. Checking if the ID is already assigned
  955. * works, because 0 isn't considered a valid ID and the memory is
  956. * 0-initialized.
  957. */
  958. if (!wdev->identifier)
  959. wdev->identifier = ++rdev->wdev_id;
  960. list_add_rcu(&wdev->list, &rdev->wiphy.wdev_list);
  961. rdev->devlist_generation++;
  962. /* can only change netns with wiphy */
  963. dev->features |= NETIF_F_NETNS_LOCAL;
  964. if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj,
  965. "phy80211")) {
  966. pr_err("failed to add phy80211 symlink to netdev!\n");
  967. }
  968. wdev->netdev = dev;
  969. #ifdef CONFIG_CFG80211_WEXT
  970. wdev->wext.default_key = -1;
  971. wdev->wext.default_mgmt_key = -1;
  972. wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
  973. #endif
  974. if (wdev->wiphy->flags & WIPHY_FLAG_PS_ON_BY_DEFAULT)
  975. wdev->ps = true;
  976. else
  977. wdev->ps = false;
  978. /* allow mac80211 to determine the timeout */
  979. wdev->ps_timeout = -1;
  980. if ((wdev->iftype == NL80211_IFTYPE_STATION ||
  981. wdev->iftype == NL80211_IFTYPE_P2P_CLIENT ||
  982. wdev->iftype == NL80211_IFTYPE_ADHOC) && !wdev->use_4addr)
  983. dev->priv_flags |= IFF_DONT_BRIDGE;
  984. INIT_WORK(&wdev->disconnect_wk, cfg80211_autodisconnect_wk);
  985. nl80211_notify_iface(rdev, wdev, NL80211_CMD_NEW_INTERFACE);
  986. break;
  987. case NETDEV_GOING_DOWN:
  988. cfg80211_leave(rdev, wdev);
  989. break;
  990. case NETDEV_DOWN:
  991. cfg80211_update_iface_num(rdev, wdev->iftype, -1);
  992. if (rdev->scan_req && rdev->scan_req->wdev == wdev) {
  993. if (WARN_ON(!rdev->scan_req->notified))
  994. rdev->scan_req->info.aborted = true;
  995. ___cfg80211_scan_done(rdev, false);
  996. }
  997. list_for_each_entry_safe(pos, tmp,
  998. &rdev->sched_scan_req_list, list) {
  999. if (WARN_ON(pos && pos->dev == wdev->netdev))
  1000. cfg80211_stop_sched_scan_req(rdev, pos, false);
  1001. }
  1002. rdev->opencount--;
  1003. wake_up(&rdev->dev_wait);
  1004. break;
  1005. case NETDEV_UP:
  1006. cfg80211_update_iface_num(rdev, wdev->iftype, 1);
  1007. wdev_lock(wdev);
  1008. switch (wdev->iftype) {
  1009. #ifdef CONFIG_CFG80211_WEXT
  1010. case NL80211_IFTYPE_ADHOC:
  1011. cfg80211_ibss_wext_join(rdev, wdev);
  1012. break;
  1013. case NL80211_IFTYPE_STATION:
  1014. cfg80211_mgd_wext_connect(rdev, wdev);
  1015. break;
  1016. #endif
  1017. #ifdef CONFIG_MAC80211_MESH
  1018. case NL80211_IFTYPE_MESH_POINT:
  1019. {
  1020. /* backward compat code... */
  1021. struct mesh_setup setup;
  1022. memcpy(&setup, &default_mesh_setup,
  1023. sizeof(setup));
  1024. /* back compat only needed for mesh_id */
  1025. setup.mesh_id = wdev->ssid;
  1026. setup.mesh_id_len = wdev->mesh_id_up_len;
  1027. if (wdev->mesh_id_up_len)
  1028. __cfg80211_join_mesh(rdev, dev,
  1029. &setup,
  1030. &default_mesh_config);
  1031. break;
  1032. }
  1033. #endif
  1034. default:
  1035. break;
  1036. }
  1037. wdev_unlock(wdev);
  1038. rdev->opencount++;
  1039. /*
  1040. * Configure power management to the driver here so that its
  1041. * correctly set also after interface type changes etc.
  1042. */
  1043. if ((wdev->iftype == NL80211_IFTYPE_STATION ||
  1044. wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) &&
  1045. rdev->ops->set_power_mgmt &&
  1046. rdev_set_power_mgmt(rdev, dev, wdev->ps,
  1047. wdev->ps_timeout)) {
  1048. /* assume this means it's off */
  1049. wdev->ps = false;
  1050. }
  1051. break;
  1052. case NETDEV_UNREGISTER:
  1053. /*
  1054. * It is possible to get NETDEV_UNREGISTER
  1055. * multiple times. To detect that, check
  1056. * that the interface is still on the list
  1057. * of registered interfaces, and only then
  1058. * remove and clean it up.
  1059. */
  1060. if (!list_empty(&wdev->list)) {
  1061. nl80211_notify_iface(rdev, wdev,
  1062. NL80211_CMD_DEL_INTERFACE);
  1063. sysfs_remove_link(&dev->dev.kobj, "phy80211");
  1064. list_del_rcu(&wdev->list);
  1065. rdev->devlist_generation++;
  1066. cfg80211_mlme_purge_registrations(wdev);
  1067. #ifdef CONFIG_CFG80211_WEXT
  1068. kzfree(wdev->wext.keys);
  1069. #endif
  1070. flush_work(&wdev->disconnect_wk);
  1071. cfg80211_cqm_config_free(wdev);
  1072. }
  1073. /*
  1074. * synchronise (so that we won't find this netdev
  1075. * from other code any more) and then clear the list
  1076. * head so that the above code can safely check for
  1077. * !list_empty() to avoid double-cleanup.
  1078. */
  1079. synchronize_rcu();
  1080. INIT_LIST_HEAD(&wdev->list);
  1081. /*
  1082. * Ensure that all events have been processed and
  1083. * freed.
  1084. */
  1085. cfg80211_process_wdev_events(wdev);
  1086. if (WARN_ON(wdev->current_bss)) {
  1087. cfg80211_unhold_bss(wdev->current_bss);
  1088. cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
  1089. wdev->current_bss = NULL;
  1090. }
  1091. break;
  1092. case NETDEV_PRE_UP:
  1093. if (!(wdev->wiphy->interface_modes & BIT(wdev->iftype)))
  1094. return notifier_from_errno(-EOPNOTSUPP);
  1095. if (rfkill_blocked(rdev->rfkill))
  1096. return notifier_from_errno(-ERFKILL);
  1097. break;
  1098. default:
  1099. return NOTIFY_DONE;
  1100. }
  1101. wireless_nlevent_flush();
  1102. return NOTIFY_OK;
  1103. }
  1104. static struct notifier_block cfg80211_netdev_notifier = {
  1105. .notifier_call = cfg80211_netdev_notifier_call,
  1106. };
  1107. static void __net_exit cfg80211_pernet_exit(struct net *net)
  1108. {
  1109. struct cfg80211_registered_device *rdev;
  1110. rtnl_lock();
  1111. list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
  1112. if (net_eq(wiphy_net(&rdev->wiphy), net))
  1113. WARN_ON(cfg80211_switch_netns(rdev, &init_net));
  1114. }
  1115. rtnl_unlock();
  1116. }
  1117. static struct pernet_operations cfg80211_pernet_ops = {
  1118. .exit = cfg80211_pernet_exit,
  1119. };
  1120. static int __init cfg80211_init(void)
  1121. {
  1122. int err;
  1123. err = register_pernet_device(&cfg80211_pernet_ops);
  1124. if (err)
  1125. goto out_fail_pernet;
  1126. err = wiphy_sysfs_init();
  1127. if (err)
  1128. goto out_fail_sysfs;
  1129. err = register_netdevice_notifier(&cfg80211_netdev_notifier);
  1130. if (err)
  1131. goto out_fail_notifier;
  1132. err = nl80211_init();
  1133. if (err)
  1134. goto out_fail_nl80211;
  1135. ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
  1136. err = regulatory_init();
  1137. if (err)
  1138. goto out_fail_reg;
  1139. cfg80211_wq = alloc_ordered_workqueue("cfg80211", WQ_MEM_RECLAIM);
  1140. if (!cfg80211_wq) {
  1141. err = -ENOMEM;
  1142. goto out_fail_wq;
  1143. }
  1144. return 0;
  1145. out_fail_wq:
  1146. regulatory_exit();
  1147. out_fail_reg:
  1148. debugfs_remove(ieee80211_debugfs_dir);
  1149. nl80211_exit();
  1150. out_fail_nl80211:
  1151. unregister_netdevice_notifier(&cfg80211_netdev_notifier);
  1152. out_fail_notifier:
  1153. wiphy_sysfs_exit();
  1154. out_fail_sysfs:
  1155. unregister_pernet_device(&cfg80211_pernet_ops);
  1156. out_fail_pernet:
  1157. return err;
  1158. }
  1159. fs_initcall(cfg80211_init);
  1160. static void __exit cfg80211_exit(void)
  1161. {
  1162. debugfs_remove(ieee80211_debugfs_dir);
  1163. nl80211_exit();
  1164. unregister_netdevice_notifier(&cfg80211_netdev_notifier);
  1165. wiphy_sysfs_exit();
  1166. regulatory_exit();
  1167. unregister_pernet_device(&cfg80211_pernet_ops);
  1168. destroy_workqueue(cfg80211_wq);
  1169. }
  1170. module_exit(cfg80211_exit);