/net/wireless/chan.c

http://github.com/mirrors/linux · C · 1246 lines · 940 code · 190 blank · 116 comment · 233 complexity · c933769e5657f811b21010fb83554872 MD5 · raw file

  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This file contains helper code to handle channel
  4. * settings and keeping track of what is possible at
  5. * any point in time.
  6. *
  7. * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
  8. * Copyright 2013-2014 Intel Mobile Communications GmbH
  9. * Copyright 2018 Intel Corporation
  10. */
  11. #include <linux/export.h>
  12. #include <net/cfg80211.h>
  13. #include "core.h"
  14. #include "rdev-ops.h"
  15. static bool cfg80211_valid_60g_freq(u32 freq)
  16. {
  17. return freq >= 58320 && freq <= 70200;
  18. }
  19. void cfg80211_chandef_create(struct cfg80211_chan_def *chandef,
  20. struct ieee80211_channel *chan,
  21. enum nl80211_channel_type chan_type)
  22. {
  23. if (WARN_ON(!chan))
  24. return;
  25. chandef->chan = chan;
  26. chandef->center_freq2 = 0;
  27. chandef->edmg.bw_config = 0;
  28. chandef->edmg.channels = 0;
  29. switch (chan_type) {
  30. case NL80211_CHAN_NO_HT:
  31. chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
  32. chandef->center_freq1 = chan->center_freq;
  33. break;
  34. case NL80211_CHAN_HT20:
  35. chandef->width = NL80211_CHAN_WIDTH_20;
  36. chandef->center_freq1 = chan->center_freq;
  37. break;
  38. case NL80211_CHAN_HT40PLUS:
  39. chandef->width = NL80211_CHAN_WIDTH_40;
  40. chandef->center_freq1 = chan->center_freq + 10;
  41. break;
  42. case NL80211_CHAN_HT40MINUS:
  43. chandef->width = NL80211_CHAN_WIDTH_40;
  44. chandef->center_freq1 = chan->center_freq - 10;
  45. break;
  46. default:
  47. WARN_ON(1);
  48. }
  49. }
  50. EXPORT_SYMBOL(cfg80211_chandef_create);
  51. static bool cfg80211_edmg_chandef_valid(const struct cfg80211_chan_def *chandef)
  52. {
  53. int max_contiguous = 0;
  54. int num_of_enabled = 0;
  55. int contiguous = 0;
  56. int i;
  57. if (!chandef->edmg.channels || !chandef->edmg.bw_config)
  58. return false;
  59. if (!cfg80211_valid_60g_freq(chandef->chan->center_freq))
  60. return false;
  61. for (i = 0; i < 6; i++) {
  62. if (chandef->edmg.channels & BIT(i)) {
  63. contiguous++;
  64. num_of_enabled++;
  65. } else {
  66. contiguous = 0;
  67. }
  68. max_contiguous = max(contiguous, max_contiguous);
  69. }
  70. /* basic verification of edmg configuration according to
  71. * IEEE P802.11ay/D4.0 section 9.4.2.251
  72. */
  73. /* check bw_config against contiguous edmg channels */
  74. switch (chandef->edmg.bw_config) {
  75. case IEEE80211_EDMG_BW_CONFIG_4:
  76. case IEEE80211_EDMG_BW_CONFIG_8:
  77. case IEEE80211_EDMG_BW_CONFIG_12:
  78. if (max_contiguous < 1)
  79. return false;
  80. break;
  81. case IEEE80211_EDMG_BW_CONFIG_5:
  82. case IEEE80211_EDMG_BW_CONFIG_9:
  83. case IEEE80211_EDMG_BW_CONFIG_13:
  84. if (max_contiguous < 2)
  85. return false;
  86. break;
  87. case IEEE80211_EDMG_BW_CONFIG_6:
  88. case IEEE80211_EDMG_BW_CONFIG_10:
  89. case IEEE80211_EDMG_BW_CONFIG_14:
  90. if (max_contiguous < 3)
  91. return false;
  92. break;
  93. case IEEE80211_EDMG_BW_CONFIG_7:
  94. case IEEE80211_EDMG_BW_CONFIG_11:
  95. case IEEE80211_EDMG_BW_CONFIG_15:
  96. if (max_contiguous < 4)
  97. return false;
  98. break;
  99. default:
  100. return false;
  101. }
  102. /* check bw_config against aggregated (non contiguous) edmg channels */
  103. switch (chandef->edmg.bw_config) {
  104. case IEEE80211_EDMG_BW_CONFIG_4:
  105. case IEEE80211_EDMG_BW_CONFIG_5:
  106. case IEEE80211_EDMG_BW_CONFIG_6:
  107. case IEEE80211_EDMG_BW_CONFIG_7:
  108. break;
  109. case IEEE80211_EDMG_BW_CONFIG_8:
  110. case IEEE80211_EDMG_BW_CONFIG_9:
  111. case IEEE80211_EDMG_BW_CONFIG_10:
  112. case IEEE80211_EDMG_BW_CONFIG_11:
  113. if (num_of_enabled < 2)
  114. return false;
  115. break;
  116. case IEEE80211_EDMG_BW_CONFIG_12:
  117. case IEEE80211_EDMG_BW_CONFIG_13:
  118. case IEEE80211_EDMG_BW_CONFIG_14:
  119. case IEEE80211_EDMG_BW_CONFIG_15:
  120. if (num_of_enabled < 4 || max_contiguous < 2)
  121. return false;
  122. break;
  123. default:
  124. return false;
  125. }
  126. return true;
  127. }
  128. bool cfg80211_chandef_valid(const struct cfg80211_chan_def *chandef)
  129. {
  130. u32 control_freq;
  131. if (!chandef->chan)
  132. return false;
  133. control_freq = chandef->chan->center_freq;
  134. switch (chandef->width) {
  135. case NL80211_CHAN_WIDTH_5:
  136. case NL80211_CHAN_WIDTH_10:
  137. case NL80211_CHAN_WIDTH_20:
  138. case NL80211_CHAN_WIDTH_20_NOHT:
  139. if (chandef->center_freq1 != control_freq)
  140. return false;
  141. if (chandef->center_freq2)
  142. return false;
  143. break;
  144. case NL80211_CHAN_WIDTH_40:
  145. if (chandef->center_freq1 != control_freq + 10 &&
  146. chandef->center_freq1 != control_freq - 10)
  147. return false;
  148. if (chandef->center_freq2)
  149. return false;
  150. break;
  151. case NL80211_CHAN_WIDTH_80P80:
  152. if (chandef->center_freq1 != control_freq + 30 &&
  153. chandef->center_freq1 != control_freq + 10 &&
  154. chandef->center_freq1 != control_freq - 10 &&
  155. chandef->center_freq1 != control_freq - 30)
  156. return false;
  157. if (!chandef->center_freq2)
  158. return false;
  159. /* adjacent is not allowed -- that's a 160 MHz channel */
  160. if (chandef->center_freq1 - chandef->center_freq2 == 80 ||
  161. chandef->center_freq2 - chandef->center_freq1 == 80)
  162. return false;
  163. break;
  164. case NL80211_CHAN_WIDTH_80:
  165. if (chandef->center_freq1 != control_freq + 30 &&
  166. chandef->center_freq1 != control_freq + 10 &&
  167. chandef->center_freq1 != control_freq - 10 &&
  168. chandef->center_freq1 != control_freq - 30)
  169. return false;
  170. if (chandef->center_freq2)
  171. return false;
  172. break;
  173. case NL80211_CHAN_WIDTH_160:
  174. if (chandef->center_freq1 != control_freq + 70 &&
  175. chandef->center_freq1 != control_freq + 50 &&
  176. chandef->center_freq1 != control_freq + 30 &&
  177. chandef->center_freq1 != control_freq + 10 &&
  178. chandef->center_freq1 != control_freq - 10 &&
  179. chandef->center_freq1 != control_freq - 30 &&
  180. chandef->center_freq1 != control_freq - 50 &&
  181. chandef->center_freq1 != control_freq - 70)
  182. return false;
  183. if (chandef->center_freq2)
  184. return false;
  185. break;
  186. default:
  187. return false;
  188. }
  189. /* channel 14 is only for IEEE 802.11b */
  190. if (chandef->center_freq1 == 2484 &&
  191. chandef->width != NL80211_CHAN_WIDTH_20_NOHT)
  192. return false;
  193. if (cfg80211_chandef_is_edmg(chandef) &&
  194. !cfg80211_edmg_chandef_valid(chandef))
  195. return false;
  196. return true;
  197. }
  198. EXPORT_SYMBOL(cfg80211_chandef_valid);
  199. static void chandef_primary_freqs(const struct cfg80211_chan_def *c,
  200. u32 *pri40, u32 *pri80)
  201. {
  202. int tmp;
  203. switch (c->width) {
  204. case NL80211_CHAN_WIDTH_40:
  205. *pri40 = c->center_freq1;
  206. *pri80 = 0;
  207. break;
  208. case NL80211_CHAN_WIDTH_80:
  209. case NL80211_CHAN_WIDTH_80P80:
  210. *pri80 = c->center_freq1;
  211. /* n_P20 */
  212. tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
  213. /* n_P40 */
  214. tmp /= 2;
  215. /* freq_P40 */
  216. *pri40 = c->center_freq1 - 20 + 40 * tmp;
  217. break;
  218. case NL80211_CHAN_WIDTH_160:
  219. /* n_P20 */
  220. tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
  221. /* n_P40 */
  222. tmp /= 2;
  223. /* freq_P40 */
  224. *pri40 = c->center_freq1 - 60 + 40 * tmp;
  225. /* n_P80 */
  226. tmp /= 2;
  227. *pri80 = c->center_freq1 - 40 + 80 * tmp;
  228. break;
  229. default:
  230. WARN_ON_ONCE(1);
  231. }
  232. }
  233. static int cfg80211_chandef_get_width(const struct cfg80211_chan_def *c)
  234. {
  235. int width;
  236. switch (c->width) {
  237. case NL80211_CHAN_WIDTH_5:
  238. width = 5;
  239. break;
  240. case NL80211_CHAN_WIDTH_10:
  241. width = 10;
  242. break;
  243. case NL80211_CHAN_WIDTH_20:
  244. case NL80211_CHAN_WIDTH_20_NOHT:
  245. width = 20;
  246. break;
  247. case NL80211_CHAN_WIDTH_40:
  248. width = 40;
  249. break;
  250. case NL80211_CHAN_WIDTH_80P80:
  251. case NL80211_CHAN_WIDTH_80:
  252. width = 80;
  253. break;
  254. case NL80211_CHAN_WIDTH_160:
  255. width = 160;
  256. break;
  257. default:
  258. WARN_ON_ONCE(1);
  259. return -1;
  260. }
  261. return width;
  262. }
  263. const struct cfg80211_chan_def *
  264. cfg80211_chandef_compatible(const struct cfg80211_chan_def *c1,
  265. const struct cfg80211_chan_def *c2)
  266. {
  267. u32 c1_pri40, c1_pri80, c2_pri40, c2_pri80;
  268. /* If they are identical, return */
  269. if (cfg80211_chandef_identical(c1, c2))
  270. return c1;
  271. /* otherwise, must have same control channel */
  272. if (c1->chan != c2->chan)
  273. return NULL;
  274. /*
  275. * If they have the same width, but aren't identical,
  276. * then they can't be compatible.
  277. */
  278. if (c1->width == c2->width)
  279. return NULL;
  280. /*
  281. * can't be compatible if one of them is 5 or 10 MHz,
  282. * but they don't have the same width.
  283. */
  284. if (c1->width == NL80211_CHAN_WIDTH_5 ||
  285. c1->width == NL80211_CHAN_WIDTH_10 ||
  286. c2->width == NL80211_CHAN_WIDTH_5 ||
  287. c2->width == NL80211_CHAN_WIDTH_10)
  288. return NULL;
  289. if (c1->width == NL80211_CHAN_WIDTH_20_NOHT ||
  290. c1->width == NL80211_CHAN_WIDTH_20)
  291. return c2;
  292. if (c2->width == NL80211_CHAN_WIDTH_20_NOHT ||
  293. c2->width == NL80211_CHAN_WIDTH_20)
  294. return c1;
  295. chandef_primary_freqs(c1, &c1_pri40, &c1_pri80);
  296. chandef_primary_freqs(c2, &c2_pri40, &c2_pri80);
  297. if (c1_pri40 != c2_pri40)
  298. return NULL;
  299. WARN_ON(!c1_pri80 && !c2_pri80);
  300. if (c1_pri80 && c2_pri80 && c1_pri80 != c2_pri80)
  301. return NULL;
  302. if (c1->width > c2->width)
  303. return c1;
  304. return c2;
  305. }
  306. EXPORT_SYMBOL(cfg80211_chandef_compatible);
  307. static void cfg80211_set_chans_dfs_state(struct wiphy *wiphy, u32 center_freq,
  308. u32 bandwidth,
  309. enum nl80211_dfs_state dfs_state)
  310. {
  311. struct ieee80211_channel *c;
  312. u32 freq;
  313. for (freq = center_freq - bandwidth/2 + 10;
  314. freq <= center_freq + bandwidth/2 - 10;
  315. freq += 20) {
  316. c = ieee80211_get_channel(wiphy, freq);
  317. if (!c || !(c->flags & IEEE80211_CHAN_RADAR))
  318. continue;
  319. c->dfs_state = dfs_state;
  320. c->dfs_state_entered = jiffies;
  321. }
  322. }
  323. void cfg80211_set_dfs_state(struct wiphy *wiphy,
  324. const struct cfg80211_chan_def *chandef,
  325. enum nl80211_dfs_state dfs_state)
  326. {
  327. int width;
  328. if (WARN_ON(!cfg80211_chandef_valid(chandef)))
  329. return;
  330. width = cfg80211_chandef_get_width(chandef);
  331. if (width < 0)
  332. return;
  333. cfg80211_set_chans_dfs_state(wiphy, chandef->center_freq1,
  334. width, dfs_state);
  335. if (!chandef->center_freq2)
  336. return;
  337. cfg80211_set_chans_dfs_state(wiphy, chandef->center_freq2,
  338. width, dfs_state);
  339. }
  340. static u32 cfg80211_get_start_freq(u32 center_freq,
  341. u32 bandwidth)
  342. {
  343. u32 start_freq;
  344. if (bandwidth <= 20)
  345. start_freq = center_freq;
  346. else
  347. start_freq = center_freq - bandwidth/2 + 10;
  348. return start_freq;
  349. }
  350. static u32 cfg80211_get_end_freq(u32 center_freq,
  351. u32 bandwidth)
  352. {
  353. u32 end_freq;
  354. if (bandwidth <= 20)
  355. end_freq = center_freq;
  356. else
  357. end_freq = center_freq + bandwidth/2 - 10;
  358. return end_freq;
  359. }
  360. static int cfg80211_get_chans_dfs_required(struct wiphy *wiphy,
  361. u32 center_freq,
  362. u32 bandwidth)
  363. {
  364. struct ieee80211_channel *c;
  365. u32 freq, start_freq, end_freq;
  366. start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
  367. end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
  368. for (freq = start_freq; freq <= end_freq; freq += 20) {
  369. c = ieee80211_get_channel(wiphy, freq);
  370. if (!c)
  371. return -EINVAL;
  372. if (c->flags & IEEE80211_CHAN_RADAR)
  373. return 1;
  374. }
  375. return 0;
  376. }
  377. int cfg80211_chandef_dfs_required(struct wiphy *wiphy,
  378. const struct cfg80211_chan_def *chandef,
  379. enum nl80211_iftype iftype)
  380. {
  381. int width;
  382. int ret;
  383. if (WARN_ON(!cfg80211_chandef_valid(chandef)))
  384. return -EINVAL;
  385. switch (iftype) {
  386. case NL80211_IFTYPE_ADHOC:
  387. case NL80211_IFTYPE_AP:
  388. case NL80211_IFTYPE_P2P_GO:
  389. case NL80211_IFTYPE_MESH_POINT:
  390. width = cfg80211_chandef_get_width(chandef);
  391. if (width < 0)
  392. return -EINVAL;
  393. ret = cfg80211_get_chans_dfs_required(wiphy,
  394. chandef->center_freq1,
  395. width);
  396. if (ret < 0)
  397. return ret;
  398. else if (ret > 0)
  399. return BIT(chandef->width);
  400. if (!chandef->center_freq2)
  401. return 0;
  402. ret = cfg80211_get_chans_dfs_required(wiphy,
  403. chandef->center_freq2,
  404. width);
  405. if (ret < 0)
  406. return ret;
  407. else if (ret > 0)
  408. return BIT(chandef->width);
  409. break;
  410. case NL80211_IFTYPE_STATION:
  411. case NL80211_IFTYPE_OCB:
  412. case NL80211_IFTYPE_P2P_CLIENT:
  413. case NL80211_IFTYPE_MONITOR:
  414. case NL80211_IFTYPE_AP_VLAN:
  415. case NL80211_IFTYPE_WDS:
  416. case NL80211_IFTYPE_P2P_DEVICE:
  417. case NL80211_IFTYPE_NAN:
  418. break;
  419. case NL80211_IFTYPE_UNSPECIFIED:
  420. case NUM_NL80211_IFTYPES:
  421. WARN_ON(1);
  422. }
  423. return 0;
  424. }
  425. EXPORT_SYMBOL(cfg80211_chandef_dfs_required);
  426. static int cfg80211_get_chans_dfs_usable(struct wiphy *wiphy,
  427. u32 center_freq,
  428. u32 bandwidth)
  429. {
  430. struct ieee80211_channel *c;
  431. u32 freq, start_freq, end_freq;
  432. int count = 0;
  433. start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
  434. end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
  435. /*
  436. * Check entire range of channels for the bandwidth.
  437. * Check all channels are DFS channels (DFS_USABLE or
  438. * DFS_AVAILABLE). Return number of usable channels
  439. * (require CAC). Allow DFS and non-DFS channel mix.
  440. */
  441. for (freq = start_freq; freq <= end_freq; freq += 20) {
  442. c = ieee80211_get_channel(wiphy, freq);
  443. if (!c)
  444. return -EINVAL;
  445. if (c->flags & IEEE80211_CHAN_DISABLED)
  446. return -EINVAL;
  447. if (c->flags & IEEE80211_CHAN_RADAR) {
  448. if (c->dfs_state == NL80211_DFS_UNAVAILABLE)
  449. return -EINVAL;
  450. if (c->dfs_state == NL80211_DFS_USABLE)
  451. count++;
  452. }
  453. }
  454. return count;
  455. }
  456. bool cfg80211_chandef_dfs_usable(struct wiphy *wiphy,
  457. const struct cfg80211_chan_def *chandef)
  458. {
  459. int width;
  460. int r1, r2 = 0;
  461. if (WARN_ON(!cfg80211_chandef_valid(chandef)))
  462. return false;
  463. width = cfg80211_chandef_get_width(chandef);
  464. if (width < 0)
  465. return false;
  466. r1 = cfg80211_get_chans_dfs_usable(wiphy, chandef->center_freq1,
  467. width);
  468. if (r1 < 0)
  469. return false;
  470. switch (chandef->width) {
  471. case NL80211_CHAN_WIDTH_80P80:
  472. WARN_ON(!chandef->center_freq2);
  473. r2 = cfg80211_get_chans_dfs_usable(wiphy,
  474. chandef->center_freq2,
  475. width);
  476. if (r2 < 0)
  477. return false;
  478. break;
  479. default:
  480. WARN_ON(chandef->center_freq2);
  481. break;
  482. }
  483. return (r1 + r2 > 0);
  484. }
  485. /*
  486. * Checks if center frequency of chan falls with in the bandwidth
  487. * range of chandef.
  488. */
  489. bool cfg80211_is_sub_chan(struct cfg80211_chan_def *chandef,
  490. struct ieee80211_channel *chan)
  491. {
  492. int width;
  493. u32 freq;
  494. if (chandef->chan->center_freq == chan->center_freq)
  495. return true;
  496. width = cfg80211_chandef_get_width(chandef);
  497. if (width <= 20)
  498. return false;
  499. for (freq = chandef->center_freq1 - width / 2 + 10;
  500. freq <= chandef->center_freq1 + width / 2 - 10; freq += 20) {
  501. if (chan->center_freq == freq)
  502. return true;
  503. }
  504. if (!chandef->center_freq2)
  505. return false;
  506. for (freq = chandef->center_freq2 - width / 2 + 10;
  507. freq <= chandef->center_freq2 + width / 2 - 10; freq += 20) {
  508. if (chan->center_freq == freq)
  509. return true;
  510. }
  511. return false;
  512. }
  513. bool cfg80211_beaconing_iface_active(struct wireless_dev *wdev)
  514. {
  515. bool active = false;
  516. ASSERT_WDEV_LOCK(wdev);
  517. if (!wdev->chandef.chan)
  518. return false;
  519. switch (wdev->iftype) {
  520. case NL80211_IFTYPE_AP:
  521. case NL80211_IFTYPE_P2P_GO:
  522. active = wdev->beacon_interval != 0;
  523. break;
  524. case NL80211_IFTYPE_ADHOC:
  525. active = wdev->ssid_len != 0;
  526. break;
  527. case NL80211_IFTYPE_MESH_POINT:
  528. active = wdev->mesh_id_len != 0;
  529. break;
  530. case NL80211_IFTYPE_STATION:
  531. case NL80211_IFTYPE_OCB:
  532. case NL80211_IFTYPE_P2P_CLIENT:
  533. case NL80211_IFTYPE_MONITOR:
  534. case NL80211_IFTYPE_AP_VLAN:
  535. case NL80211_IFTYPE_WDS:
  536. case NL80211_IFTYPE_P2P_DEVICE:
  537. /* Can NAN type be considered as beaconing interface? */
  538. case NL80211_IFTYPE_NAN:
  539. break;
  540. case NL80211_IFTYPE_UNSPECIFIED:
  541. case NUM_NL80211_IFTYPES:
  542. WARN_ON(1);
  543. }
  544. return active;
  545. }
  546. static bool cfg80211_is_wiphy_oper_chan(struct wiphy *wiphy,
  547. struct ieee80211_channel *chan)
  548. {
  549. struct wireless_dev *wdev;
  550. list_for_each_entry(wdev, &wiphy->wdev_list, list) {
  551. wdev_lock(wdev);
  552. if (!cfg80211_beaconing_iface_active(wdev)) {
  553. wdev_unlock(wdev);
  554. continue;
  555. }
  556. if (cfg80211_is_sub_chan(&wdev->chandef, chan)) {
  557. wdev_unlock(wdev);
  558. return true;
  559. }
  560. wdev_unlock(wdev);
  561. }
  562. return false;
  563. }
  564. bool cfg80211_any_wiphy_oper_chan(struct wiphy *wiphy,
  565. struct ieee80211_channel *chan)
  566. {
  567. struct cfg80211_registered_device *rdev;
  568. ASSERT_RTNL();
  569. if (!(chan->flags & IEEE80211_CHAN_RADAR))
  570. return false;
  571. list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
  572. if (!reg_dfs_domain_same(wiphy, &rdev->wiphy))
  573. continue;
  574. if (cfg80211_is_wiphy_oper_chan(&rdev->wiphy, chan))
  575. return true;
  576. }
  577. return false;
  578. }
  579. static bool cfg80211_get_chans_dfs_available(struct wiphy *wiphy,
  580. u32 center_freq,
  581. u32 bandwidth)
  582. {
  583. struct ieee80211_channel *c;
  584. u32 freq, start_freq, end_freq;
  585. bool dfs_offload;
  586. dfs_offload = wiphy_ext_feature_isset(wiphy,
  587. NL80211_EXT_FEATURE_DFS_OFFLOAD);
  588. start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
  589. end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
  590. /*
  591. * Check entire range of channels for the bandwidth.
  592. * If any channel in between is disabled or has not
  593. * had gone through CAC return false
  594. */
  595. for (freq = start_freq; freq <= end_freq; freq += 20) {
  596. c = ieee80211_get_channel(wiphy, freq);
  597. if (!c)
  598. return false;
  599. if (c->flags & IEEE80211_CHAN_DISABLED)
  600. return false;
  601. if ((c->flags & IEEE80211_CHAN_RADAR) &&
  602. (c->dfs_state != NL80211_DFS_AVAILABLE) &&
  603. !(c->dfs_state == NL80211_DFS_USABLE && dfs_offload))
  604. return false;
  605. }
  606. return true;
  607. }
  608. static bool cfg80211_chandef_dfs_available(struct wiphy *wiphy,
  609. const struct cfg80211_chan_def *chandef)
  610. {
  611. int width;
  612. int r;
  613. if (WARN_ON(!cfg80211_chandef_valid(chandef)))
  614. return false;
  615. width = cfg80211_chandef_get_width(chandef);
  616. if (width < 0)
  617. return false;
  618. r = cfg80211_get_chans_dfs_available(wiphy, chandef->center_freq1,
  619. width);
  620. /* If any of channels unavailable for cf1 just return */
  621. if (!r)
  622. return r;
  623. switch (chandef->width) {
  624. case NL80211_CHAN_WIDTH_80P80:
  625. WARN_ON(!chandef->center_freq2);
  626. r = cfg80211_get_chans_dfs_available(wiphy,
  627. chandef->center_freq2,
  628. width);
  629. break;
  630. default:
  631. WARN_ON(chandef->center_freq2);
  632. break;
  633. }
  634. return r;
  635. }
  636. static unsigned int cfg80211_get_chans_dfs_cac_time(struct wiphy *wiphy,
  637. u32 center_freq,
  638. u32 bandwidth)
  639. {
  640. struct ieee80211_channel *c;
  641. u32 start_freq, end_freq, freq;
  642. unsigned int dfs_cac_ms = 0;
  643. start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
  644. end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
  645. for (freq = start_freq; freq <= end_freq; freq += 20) {
  646. c = ieee80211_get_channel(wiphy, freq);
  647. if (!c)
  648. return 0;
  649. if (c->flags & IEEE80211_CHAN_DISABLED)
  650. return 0;
  651. if (!(c->flags & IEEE80211_CHAN_RADAR))
  652. continue;
  653. if (c->dfs_cac_ms > dfs_cac_ms)
  654. dfs_cac_ms = c->dfs_cac_ms;
  655. }
  656. return dfs_cac_ms;
  657. }
  658. unsigned int
  659. cfg80211_chandef_dfs_cac_time(struct wiphy *wiphy,
  660. const struct cfg80211_chan_def *chandef)
  661. {
  662. int width;
  663. unsigned int t1 = 0, t2 = 0;
  664. if (WARN_ON(!cfg80211_chandef_valid(chandef)))
  665. return 0;
  666. width = cfg80211_chandef_get_width(chandef);
  667. if (width < 0)
  668. return 0;
  669. t1 = cfg80211_get_chans_dfs_cac_time(wiphy,
  670. chandef->center_freq1,
  671. width);
  672. if (!chandef->center_freq2)
  673. return t1;
  674. t2 = cfg80211_get_chans_dfs_cac_time(wiphy,
  675. chandef->center_freq2,
  676. width);
  677. return max(t1, t2);
  678. }
  679. static bool cfg80211_secondary_chans_ok(struct wiphy *wiphy,
  680. u32 center_freq, u32 bandwidth,
  681. u32 prohibited_flags)
  682. {
  683. struct ieee80211_channel *c;
  684. u32 freq, start_freq, end_freq;
  685. start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
  686. end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
  687. for (freq = start_freq; freq <= end_freq; freq += 20) {
  688. c = ieee80211_get_channel(wiphy, freq);
  689. if (!c || c->flags & prohibited_flags)
  690. return false;
  691. }
  692. return true;
  693. }
  694. /* check if the operating channels are valid and supported */
  695. static bool cfg80211_edmg_usable(struct wiphy *wiphy, u8 edmg_channels,
  696. enum ieee80211_edmg_bw_config edmg_bw_config,
  697. int primary_channel,
  698. struct ieee80211_edmg *edmg_cap)
  699. {
  700. struct ieee80211_channel *chan;
  701. int i, freq;
  702. int channels_counter = 0;
  703. if (!edmg_channels && !edmg_bw_config)
  704. return true;
  705. if ((!edmg_channels && edmg_bw_config) ||
  706. (edmg_channels && !edmg_bw_config))
  707. return false;
  708. if (!(edmg_channels & BIT(primary_channel - 1)))
  709. return false;
  710. /* 60GHz channels 1..6 */
  711. for (i = 0; i < 6; i++) {
  712. if (!(edmg_channels & BIT(i)))
  713. continue;
  714. if (!(edmg_cap->channels & BIT(i)))
  715. return false;
  716. channels_counter++;
  717. freq = ieee80211_channel_to_frequency(i + 1,
  718. NL80211_BAND_60GHZ);
  719. chan = ieee80211_get_channel(wiphy, freq);
  720. if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
  721. return false;
  722. }
  723. /* IEEE802.11 allows max 4 channels */
  724. if (channels_counter > 4)
  725. return false;
  726. /* check bw_config is a subset of what driver supports
  727. * (see IEEE P802.11ay/D4.0 section 9.4.2.251, Table 13)
  728. */
  729. if ((edmg_bw_config % 4) > (edmg_cap->bw_config % 4))
  730. return false;
  731. if (edmg_bw_config > edmg_cap->bw_config)
  732. return false;
  733. return true;
  734. }
  735. bool cfg80211_chandef_usable(struct wiphy *wiphy,
  736. const struct cfg80211_chan_def *chandef,
  737. u32 prohibited_flags)
  738. {
  739. struct ieee80211_sta_ht_cap *ht_cap;
  740. struct ieee80211_sta_vht_cap *vht_cap;
  741. struct ieee80211_edmg *edmg_cap;
  742. u32 width, control_freq, cap;
  743. if (WARN_ON(!cfg80211_chandef_valid(chandef)))
  744. return false;
  745. ht_cap = &wiphy->bands[chandef->chan->band]->ht_cap;
  746. vht_cap = &wiphy->bands[chandef->chan->band]->vht_cap;
  747. edmg_cap = &wiphy->bands[chandef->chan->band]->edmg_cap;
  748. if (edmg_cap->channels &&
  749. !cfg80211_edmg_usable(wiphy,
  750. chandef->edmg.channels,
  751. chandef->edmg.bw_config,
  752. chandef->chan->hw_value,
  753. edmg_cap))
  754. return false;
  755. control_freq = chandef->chan->center_freq;
  756. switch (chandef->width) {
  757. case NL80211_CHAN_WIDTH_5:
  758. width = 5;
  759. break;
  760. case NL80211_CHAN_WIDTH_10:
  761. prohibited_flags |= IEEE80211_CHAN_NO_10MHZ;
  762. width = 10;
  763. break;
  764. case NL80211_CHAN_WIDTH_20:
  765. if (!ht_cap->ht_supported)
  766. return false;
  767. /* fall through */
  768. case NL80211_CHAN_WIDTH_20_NOHT:
  769. prohibited_flags |= IEEE80211_CHAN_NO_20MHZ;
  770. width = 20;
  771. break;
  772. case NL80211_CHAN_WIDTH_40:
  773. width = 40;
  774. if (!ht_cap->ht_supported)
  775. return false;
  776. if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
  777. ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT)
  778. return false;
  779. if (chandef->center_freq1 < control_freq &&
  780. chandef->chan->flags & IEEE80211_CHAN_NO_HT40MINUS)
  781. return false;
  782. if (chandef->center_freq1 > control_freq &&
  783. chandef->chan->flags & IEEE80211_CHAN_NO_HT40PLUS)
  784. return false;
  785. break;
  786. case NL80211_CHAN_WIDTH_80P80:
  787. cap = vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
  788. if (cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)
  789. return false;
  790. /* fall through */
  791. case NL80211_CHAN_WIDTH_80:
  792. if (!vht_cap->vht_supported)
  793. return false;
  794. prohibited_flags |= IEEE80211_CHAN_NO_80MHZ;
  795. width = 80;
  796. break;
  797. case NL80211_CHAN_WIDTH_160:
  798. if (!vht_cap->vht_supported)
  799. return false;
  800. cap = vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
  801. if (cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ &&
  802. cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)
  803. return false;
  804. prohibited_flags |= IEEE80211_CHAN_NO_160MHZ;
  805. width = 160;
  806. break;
  807. default:
  808. WARN_ON_ONCE(1);
  809. return false;
  810. }
  811. /*
  812. * TODO: What if there are only certain 80/160/80+80 MHz channels
  813. * allowed by the driver, or only certain combinations?
  814. * For 40 MHz the driver can set the NO_HT40 flags, but for
  815. * 80/160 MHz and in particular 80+80 MHz this isn't really
  816. * feasible and we only have NO_80MHZ/NO_160MHZ so far but
  817. * no way to cover 80+80 MHz or more complex restrictions.
  818. * Note that such restrictions also need to be advertised to
  819. * userspace, for example for P2P channel selection.
  820. */
  821. if (width > 20)
  822. prohibited_flags |= IEEE80211_CHAN_NO_OFDM;
  823. /* 5 and 10 MHz are only defined for the OFDM PHY */
  824. if (width < 20)
  825. prohibited_flags |= IEEE80211_CHAN_NO_OFDM;
  826. if (!cfg80211_secondary_chans_ok(wiphy, chandef->center_freq1,
  827. width, prohibited_flags))
  828. return false;
  829. if (!chandef->center_freq2)
  830. return true;
  831. return cfg80211_secondary_chans_ok(wiphy, chandef->center_freq2,
  832. width, prohibited_flags);
  833. }
  834. EXPORT_SYMBOL(cfg80211_chandef_usable);
  835. /*
  836. * Check if the channel can be used under permissive conditions mandated by
  837. * some regulatory bodies, i.e., the channel is marked with
  838. * IEEE80211_CHAN_IR_CONCURRENT and there is an additional station interface
  839. * associated to an AP on the same channel or on the same UNII band
  840. * (assuming that the AP is an authorized master).
  841. * In addition allow operation on a channel on which indoor operation is
  842. * allowed, iff we are currently operating in an indoor environment.
  843. */
  844. static bool cfg80211_ir_permissive_chan(struct wiphy *wiphy,
  845. enum nl80211_iftype iftype,
  846. struct ieee80211_channel *chan)
  847. {
  848. struct wireless_dev *wdev;
  849. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  850. ASSERT_RTNL();
  851. if (!IS_ENABLED(CONFIG_CFG80211_REG_RELAX_NO_IR) ||
  852. !(wiphy->regulatory_flags & REGULATORY_ENABLE_RELAX_NO_IR))
  853. return false;
  854. /* only valid for GO and TDLS off-channel (station/p2p-CL) */
  855. if (iftype != NL80211_IFTYPE_P2P_GO &&
  856. iftype != NL80211_IFTYPE_STATION &&
  857. iftype != NL80211_IFTYPE_P2P_CLIENT)
  858. return false;
  859. if (regulatory_indoor_allowed() &&
  860. (chan->flags & IEEE80211_CHAN_INDOOR_ONLY))
  861. return true;
  862. if (!(chan->flags & IEEE80211_CHAN_IR_CONCURRENT))
  863. return false;
  864. /*
  865. * Generally, it is possible to rely on another device/driver to allow
  866. * the IR concurrent relaxation, however, since the device can further
  867. * enforce the relaxation (by doing a similar verifications as this),
  868. * and thus fail the GO instantiation, consider only the interfaces of
  869. * the current registered device.
  870. */
  871. list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
  872. struct ieee80211_channel *other_chan = NULL;
  873. int r1, r2;
  874. wdev_lock(wdev);
  875. if (wdev->iftype == NL80211_IFTYPE_STATION &&
  876. wdev->current_bss)
  877. other_chan = wdev->current_bss->pub.channel;
  878. /*
  879. * If a GO already operates on the same GO_CONCURRENT channel,
  880. * this one (maybe the same one) can beacon as well. We allow
  881. * the operation even if the station we relied on with
  882. * GO_CONCURRENT is disconnected now. But then we must make sure
  883. * we're not outdoor on an indoor-only channel.
  884. */
  885. if (iftype == NL80211_IFTYPE_P2P_GO &&
  886. wdev->iftype == NL80211_IFTYPE_P2P_GO &&
  887. wdev->beacon_interval &&
  888. !(chan->flags & IEEE80211_CHAN_INDOOR_ONLY))
  889. other_chan = wdev->chandef.chan;
  890. wdev_unlock(wdev);
  891. if (!other_chan)
  892. continue;
  893. if (chan == other_chan)
  894. return true;
  895. if (chan->band != NL80211_BAND_5GHZ &&
  896. chan->band != NL80211_BAND_6GHZ)
  897. continue;
  898. r1 = cfg80211_get_unii(chan->center_freq);
  899. r2 = cfg80211_get_unii(other_chan->center_freq);
  900. if (r1 != -EINVAL && r1 == r2) {
  901. /*
  902. * At some locations channels 149-165 are considered a
  903. * bundle, but at other locations, e.g., Indonesia,
  904. * channels 149-161 are considered a bundle while
  905. * channel 165 is left out and considered to be in a
  906. * different bundle. Thus, in case that there is a
  907. * station interface connected to an AP on channel 165,
  908. * it is assumed that channels 149-161 are allowed for
  909. * GO operations. However, having a station interface
  910. * connected to an AP on channels 149-161, does not
  911. * allow GO operation on channel 165.
  912. */
  913. if (chan->center_freq == 5825 &&
  914. other_chan->center_freq != 5825)
  915. continue;
  916. return true;
  917. }
  918. }
  919. return false;
  920. }
  921. static bool _cfg80211_reg_can_beacon(struct wiphy *wiphy,
  922. struct cfg80211_chan_def *chandef,
  923. enum nl80211_iftype iftype,
  924. bool check_no_ir)
  925. {
  926. bool res;
  927. u32 prohibited_flags = IEEE80211_CHAN_DISABLED |
  928. IEEE80211_CHAN_RADAR;
  929. trace_cfg80211_reg_can_beacon(wiphy, chandef, iftype, check_no_ir);
  930. if (check_no_ir)
  931. prohibited_flags |= IEEE80211_CHAN_NO_IR;
  932. if (cfg80211_chandef_dfs_required(wiphy, chandef, iftype) > 0 &&
  933. cfg80211_chandef_dfs_available(wiphy, chandef)) {
  934. /* We can skip IEEE80211_CHAN_NO_IR if chandef dfs available */
  935. prohibited_flags = IEEE80211_CHAN_DISABLED;
  936. }
  937. res = cfg80211_chandef_usable(wiphy, chandef, prohibited_flags);
  938. trace_cfg80211_return_bool(res);
  939. return res;
  940. }
  941. bool cfg80211_reg_can_beacon(struct wiphy *wiphy,
  942. struct cfg80211_chan_def *chandef,
  943. enum nl80211_iftype iftype)
  944. {
  945. return _cfg80211_reg_can_beacon(wiphy, chandef, iftype, true);
  946. }
  947. EXPORT_SYMBOL(cfg80211_reg_can_beacon);
  948. bool cfg80211_reg_can_beacon_relax(struct wiphy *wiphy,
  949. struct cfg80211_chan_def *chandef,
  950. enum nl80211_iftype iftype)
  951. {
  952. bool check_no_ir;
  953. ASSERT_RTNL();
  954. /*
  955. * Under certain conditions suggested by some regulatory bodies a
  956. * GO/STA can IR on channels marked with IEEE80211_NO_IR. Set this flag
  957. * only if such relaxations are not enabled and the conditions are not
  958. * met.
  959. */
  960. check_no_ir = !cfg80211_ir_permissive_chan(wiphy, iftype,
  961. chandef->chan);
  962. return _cfg80211_reg_can_beacon(wiphy, chandef, iftype, check_no_ir);
  963. }
  964. EXPORT_SYMBOL(cfg80211_reg_can_beacon_relax);
  965. int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev,
  966. struct cfg80211_chan_def *chandef)
  967. {
  968. if (!rdev->ops->set_monitor_channel)
  969. return -EOPNOTSUPP;
  970. if (!cfg80211_has_monitors_only(rdev))
  971. return -EBUSY;
  972. return rdev_set_monitor_channel(rdev, chandef);
  973. }
  974. void
  975. cfg80211_get_chan_state(struct wireless_dev *wdev,
  976. struct ieee80211_channel **chan,
  977. enum cfg80211_chan_mode *chanmode,
  978. u8 *radar_detect)
  979. {
  980. int ret;
  981. *chan = NULL;
  982. *chanmode = CHAN_MODE_UNDEFINED;
  983. ASSERT_WDEV_LOCK(wdev);
  984. if (wdev->netdev && !netif_running(wdev->netdev))
  985. return;
  986. switch (wdev->iftype) {
  987. case NL80211_IFTYPE_ADHOC:
  988. if (wdev->current_bss) {
  989. *chan = wdev->current_bss->pub.channel;
  990. *chanmode = (wdev->ibss_fixed &&
  991. !wdev->ibss_dfs_possible)
  992. ? CHAN_MODE_SHARED
  993. : CHAN_MODE_EXCLUSIVE;
  994. /* consider worst-case - IBSS can try to return to the
  995. * original user-specified channel as creator */
  996. if (wdev->ibss_dfs_possible)
  997. *radar_detect |= BIT(wdev->chandef.width);
  998. return;
  999. }
  1000. break;
  1001. case NL80211_IFTYPE_STATION:
  1002. case NL80211_IFTYPE_P2P_CLIENT:
  1003. if (wdev->current_bss) {
  1004. *chan = wdev->current_bss->pub.channel;
  1005. *chanmode = CHAN_MODE_SHARED;
  1006. return;
  1007. }
  1008. break;
  1009. case NL80211_IFTYPE_AP:
  1010. case NL80211_IFTYPE_P2P_GO:
  1011. if (wdev->cac_started) {
  1012. *chan = wdev->chandef.chan;
  1013. *chanmode = CHAN_MODE_SHARED;
  1014. *radar_detect |= BIT(wdev->chandef.width);
  1015. } else if (wdev->beacon_interval) {
  1016. *chan = wdev->chandef.chan;
  1017. *chanmode = CHAN_MODE_SHARED;
  1018. ret = cfg80211_chandef_dfs_required(wdev->wiphy,
  1019. &wdev->chandef,
  1020. wdev->iftype);
  1021. WARN_ON(ret < 0);
  1022. if (ret > 0)
  1023. *radar_detect |= BIT(wdev->chandef.width);
  1024. }
  1025. return;
  1026. case NL80211_IFTYPE_MESH_POINT:
  1027. if (wdev->mesh_id_len) {
  1028. *chan = wdev->chandef.chan;
  1029. *chanmode = CHAN_MODE_SHARED;
  1030. ret = cfg80211_chandef_dfs_required(wdev->wiphy,
  1031. &wdev->chandef,
  1032. wdev->iftype);
  1033. WARN_ON(ret < 0);
  1034. if (ret > 0)
  1035. *radar_detect |= BIT(wdev->chandef.width);
  1036. }
  1037. return;
  1038. case NL80211_IFTYPE_OCB:
  1039. if (wdev->chandef.chan) {
  1040. *chan = wdev->chandef.chan;
  1041. *chanmode = CHAN_MODE_SHARED;
  1042. return;
  1043. }
  1044. break;
  1045. case NL80211_IFTYPE_MONITOR:
  1046. case NL80211_IFTYPE_AP_VLAN:
  1047. case NL80211_IFTYPE_WDS:
  1048. case NL80211_IFTYPE_P2P_DEVICE:
  1049. case NL80211_IFTYPE_NAN:
  1050. /* these interface types don't really have a channel */
  1051. return;
  1052. case NL80211_IFTYPE_UNSPECIFIED:
  1053. case NUM_NL80211_IFTYPES:
  1054. WARN_ON(1);
  1055. }
  1056. }