core.c 30 KB

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