chan.c 26 KB

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