chan.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531
  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 <[email protected]>
  8. * Copyright 2013-2014 Intel Mobile Communications GmbH
  9. * Copyright 2018-2022 Intel Corporation
  10. */
  11. #include <linux/export.h>
  12. #include <linux/bitfield.h>
  13. #include <net/cfg80211.h>
  14. #include "core.h"
  15. #include "rdev-ops.h"
  16. static bool cfg80211_valid_60g_freq(u32 freq)
  17. {
  18. return freq >= 58320 && freq <= 70200;
  19. }
  20. void cfg80211_chandef_create(struct cfg80211_chan_def *chandef,
  21. struct ieee80211_channel *chan,
  22. enum nl80211_channel_type chan_type)
  23. {
  24. if (WARN_ON(!chan))
  25. return;
  26. chandef->chan = chan;
  27. chandef->freq1_offset = chan->freq_offset;
  28. chandef->center_freq2 = 0;
  29. chandef->edmg.bw_config = 0;
  30. chandef->edmg.channels = 0;
  31. switch (chan_type) {
  32. case NL80211_CHAN_NO_HT:
  33. chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
  34. chandef->center_freq1 = chan->center_freq;
  35. break;
  36. case NL80211_CHAN_HT20:
  37. chandef->width = NL80211_CHAN_WIDTH_20;
  38. chandef->center_freq1 = chan->center_freq;
  39. break;
  40. case NL80211_CHAN_HT40PLUS:
  41. chandef->width = NL80211_CHAN_WIDTH_40;
  42. chandef->center_freq1 = chan->center_freq + 10;
  43. break;
  44. case NL80211_CHAN_HT40MINUS:
  45. chandef->width = NL80211_CHAN_WIDTH_40;
  46. chandef->center_freq1 = chan->center_freq - 10;
  47. break;
  48. default:
  49. WARN_ON(1);
  50. }
  51. }
  52. EXPORT_SYMBOL(cfg80211_chandef_create);
  53. static bool cfg80211_edmg_chandef_valid(const struct cfg80211_chan_def *chandef)
  54. {
  55. int max_contiguous = 0;
  56. int num_of_enabled = 0;
  57. int contiguous = 0;
  58. int i;
  59. if (!chandef->edmg.channels || !chandef->edmg.bw_config)
  60. return false;
  61. if (!cfg80211_valid_60g_freq(chandef->chan->center_freq))
  62. return false;
  63. for (i = 0; i < 6; i++) {
  64. if (chandef->edmg.channels & BIT(i)) {
  65. contiguous++;
  66. num_of_enabled++;
  67. } else {
  68. contiguous = 0;
  69. }
  70. max_contiguous = max(contiguous, max_contiguous);
  71. }
  72. /* basic verification of edmg configuration according to
  73. * IEEE P802.11ay/D4.0 section 9.4.2.251
  74. */
  75. /* check bw_config against contiguous edmg channels */
  76. switch (chandef->edmg.bw_config) {
  77. case IEEE80211_EDMG_BW_CONFIG_4:
  78. case IEEE80211_EDMG_BW_CONFIG_8:
  79. case IEEE80211_EDMG_BW_CONFIG_12:
  80. if (max_contiguous < 1)
  81. return false;
  82. break;
  83. case IEEE80211_EDMG_BW_CONFIG_5:
  84. case IEEE80211_EDMG_BW_CONFIG_9:
  85. case IEEE80211_EDMG_BW_CONFIG_13:
  86. if (max_contiguous < 2)
  87. return false;
  88. break;
  89. case IEEE80211_EDMG_BW_CONFIG_6:
  90. case IEEE80211_EDMG_BW_CONFIG_10:
  91. case IEEE80211_EDMG_BW_CONFIG_14:
  92. if (max_contiguous < 3)
  93. return false;
  94. break;
  95. case IEEE80211_EDMG_BW_CONFIG_7:
  96. case IEEE80211_EDMG_BW_CONFIG_11:
  97. case IEEE80211_EDMG_BW_CONFIG_15:
  98. if (max_contiguous < 4)
  99. return false;
  100. break;
  101. default:
  102. return false;
  103. }
  104. /* check bw_config against aggregated (non contiguous) edmg channels */
  105. switch (chandef->edmg.bw_config) {
  106. case IEEE80211_EDMG_BW_CONFIG_4:
  107. case IEEE80211_EDMG_BW_CONFIG_5:
  108. case IEEE80211_EDMG_BW_CONFIG_6:
  109. case IEEE80211_EDMG_BW_CONFIG_7:
  110. break;
  111. case IEEE80211_EDMG_BW_CONFIG_8:
  112. case IEEE80211_EDMG_BW_CONFIG_9:
  113. case IEEE80211_EDMG_BW_CONFIG_10:
  114. case IEEE80211_EDMG_BW_CONFIG_11:
  115. if (num_of_enabled < 2)
  116. return false;
  117. break;
  118. case IEEE80211_EDMG_BW_CONFIG_12:
  119. case IEEE80211_EDMG_BW_CONFIG_13:
  120. case IEEE80211_EDMG_BW_CONFIG_14:
  121. case IEEE80211_EDMG_BW_CONFIG_15:
  122. if (num_of_enabled < 4 || max_contiguous < 2)
  123. return false;
  124. break;
  125. default:
  126. return false;
  127. }
  128. return true;
  129. }
  130. static int nl80211_chan_width_to_mhz(enum nl80211_chan_width chan_width)
  131. {
  132. int mhz;
  133. switch (chan_width) {
  134. case NL80211_CHAN_WIDTH_1:
  135. mhz = 1;
  136. break;
  137. case NL80211_CHAN_WIDTH_2:
  138. mhz = 2;
  139. break;
  140. case NL80211_CHAN_WIDTH_4:
  141. mhz = 4;
  142. break;
  143. case NL80211_CHAN_WIDTH_8:
  144. mhz = 8;
  145. break;
  146. case NL80211_CHAN_WIDTH_16:
  147. mhz = 16;
  148. break;
  149. case NL80211_CHAN_WIDTH_5:
  150. mhz = 5;
  151. break;
  152. case NL80211_CHAN_WIDTH_10:
  153. mhz = 10;
  154. break;
  155. case NL80211_CHAN_WIDTH_20:
  156. case NL80211_CHAN_WIDTH_20_NOHT:
  157. mhz = 20;
  158. break;
  159. case NL80211_CHAN_WIDTH_40:
  160. mhz = 40;
  161. break;
  162. case NL80211_CHAN_WIDTH_80P80:
  163. case NL80211_CHAN_WIDTH_80:
  164. mhz = 80;
  165. break;
  166. case NL80211_CHAN_WIDTH_160:
  167. mhz = 160;
  168. break;
  169. case NL80211_CHAN_WIDTH_320:
  170. mhz = 320;
  171. break;
  172. default:
  173. WARN_ON_ONCE(1);
  174. return -1;
  175. }
  176. return mhz;
  177. }
  178. static int cfg80211_chandef_get_width(const struct cfg80211_chan_def *c)
  179. {
  180. return nl80211_chan_width_to_mhz(c->width);
  181. }
  182. bool cfg80211_chandef_valid(const struct cfg80211_chan_def *chandef)
  183. {
  184. u32 control_freq, oper_freq;
  185. int oper_width, control_width;
  186. if (!chandef->chan)
  187. return false;
  188. if (chandef->freq1_offset >= 1000)
  189. return false;
  190. control_freq = chandef->chan->center_freq;
  191. switch (chandef->width) {
  192. case NL80211_CHAN_WIDTH_5:
  193. case NL80211_CHAN_WIDTH_10:
  194. case NL80211_CHAN_WIDTH_20:
  195. case NL80211_CHAN_WIDTH_20_NOHT:
  196. if (ieee80211_chandef_to_khz(chandef) !=
  197. ieee80211_channel_to_khz(chandef->chan))
  198. return false;
  199. if (chandef->center_freq2)
  200. return false;
  201. break;
  202. case NL80211_CHAN_WIDTH_1:
  203. case NL80211_CHAN_WIDTH_2:
  204. case NL80211_CHAN_WIDTH_4:
  205. case NL80211_CHAN_WIDTH_8:
  206. case NL80211_CHAN_WIDTH_16:
  207. if (chandef->chan->band != NL80211_BAND_S1GHZ)
  208. return false;
  209. control_freq = ieee80211_channel_to_khz(chandef->chan);
  210. oper_freq = ieee80211_chandef_to_khz(chandef);
  211. control_width = nl80211_chan_width_to_mhz(
  212. ieee80211_s1g_channel_width(
  213. chandef->chan));
  214. oper_width = cfg80211_chandef_get_width(chandef);
  215. if (oper_width < 0 || control_width < 0)
  216. return false;
  217. if (chandef->center_freq2)
  218. return false;
  219. if (control_freq + MHZ_TO_KHZ(control_width) / 2 >
  220. oper_freq + MHZ_TO_KHZ(oper_width) / 2)
  221. return false;
  222. if (control_freq - MHZ_TO_KHZ(control_width) / 2 <
  223. oper_freq - MHZ_TO_KHZ(oper_width) / 2)
  224. return false;
  225. break;
  226. case NL80211_CHAN_WIDTH_80P80:
  227. if (!chandef->center_freq2)
  228. return false;
  229. /* adjacent is not allowed -- that's a 160 MHz channel */
  230. if (chandef->center_freq1 - chandef->center_freq2 == 80 ||
  231. chandef->center_freq2 - chandef->center_freq1 == 80)
  232. return false;
  233. break;
  234. default:
  235. if (chandef->center_freq2)
  236. return false;
  237. break;
  238. }
  239. switch (chandef->width) {
  240. case NL80211_CHAN_WIDTH_5:
  241. case NL80211_CHAN_WIDTH_10:
  242. case NL80211_CHAN_WIDTH_20:
  243. case NL80211_CHAN_WIDTH_20_NOHT:
  244. case NL80211_CHAN_WIDTH_1:
  245. case NL80211_CHAN_WIDTH_2:
  246. case NL80211_CHAN_WIDTH_4:
  247. case NL80211_CHAN_WIDTH_8:
  248. case NL80211_CHAN_WIDTH_16:
  249. /* all checked above */
  250. break;
  251. case NL80211_CHAN_WIDTH_320:
  252. if (chandef->center_freq1 == control_freq + 150 ||
  253. chandef->center_freq1 == control_freq + 130 ||
  254. chandef->center_freq1 == control_freq + 110 ||
  255. chandef->center_freq1 == control_freq + 90 ||
  256. chandef->center_freq1 == control_freq - 90 ||
  257. chandef->center_freq1 == control_freq - 110 ||
  258. chandef->center_freq1 == control_freq - 130 ||
  259. chandef->center_freq1 == control_freq - 150)
  260. break;
  261. fallthrough;
  262. case NL80211_CHAN_WIDTH_160:
  263. if (chandef->center_freq1 == control_freq + 70 ||
  264. chandef->center_freq1 == control_freq + 50 ||
  265. chandef->center_freq1 == control_freq - 50 ||
  266. chandef->center_freq1 == control_freq - 70)
  267. break;
  268. fallthrough;
  269. case NL80211_CHAN_WIDTH_80P80:
  270. case NL80211_CHAN_WIDTH_80:
  271. if (chandef->center_freq1 == control_freq + 30 ||
  272. chandef->center_freq1 == control_freq - 30)
  273. break;
  274. fallthrough;
  275. case NL80211_CHAN_WIDTH_40:
  276. if (chandef->center_freq1 == control_freq + 10 ||
  277. chandef->center_freq1 == control_freq - 10)
  278. break;
  279. fallthrough;
  280. default:
  281. return false;
  282. }
  283. /* channel 14 is only for IEEE 802.11b */
  284. if (chandef->center_freq1 == 2484 &&
  285. chandef->width != NL80211_CHAN_WIDTH_20_NOHT)
  286. return false;
  287. if (cfg80211_chandef_is_edmg(chandef) &&
  288. !cfg80211_edmg_chandef_valid(chandef))
  289. return false;
  290. return true;
  291. }
  292. EXPORT_SYMBOL(cfg80211_chandef_valid);
  293. static void chandef_primary_freqs(const struct cfg80211_chan_def *c,
  294. u32 *pri40, u32 *pri80, u32 *pri160)
  295. {
  296. int tmp;
  297. switch (c->width) {
  298. case NL80211_CHAN_WIDTH_40:
  299. *pri40 = c->center_freq1;
  300. *pri80 = 0;
  301. *pri160 = 0;
  302. break;
  303. case NL80211_CHAN_WIDTH_80:
  304. case NL80211_CHAN_WIDTH_80P80:
  305. *pri160 = 0;
  306. *pri80 = c->center_freq1;
  307. /* n_P20 */
  308. tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
  309. /* n_P40 */
  310. tmp /= 2;
  311. /* freq_P40 */
  312. *pri40 = c->center_freq1 - 20 + 40 * tmp;
  313. break;
  314. case NL80211_CHAN_WIDTH_160:
  315. *pri160 = c->center_freq1;
  316. /* n_P20 */
  317. tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
  318. /* n_P40 */
  319. tmp /= 2;
  320. /* freq_P40 */
  321. *pri40 = c->center_freq1 - 60 + 40 * tmp;
  322. /* n_P80 */
  323. tmp /= 2;
  324. *pri80 = c->center_freq1 - 40 + 80 * tmp;
  325. break;
  326. case NL80211_CHAN_WIDTH_320:
  327. /* n_P20 */
  328. tmp = (150 + c->chan->center_freq - c->center_freq1) / 20;
  329. /* n_P40 */
  330. tmp /= 2;
  331. /* freq_P40 */
  332. *pri40 = c->center_freq1 - 140 + 40 * tmp;
  333. /* n_P80 */
  334. tmp /= 2;
  335. *pri80 = c->center_freq1 - 120 + 80 * tmp;
  336. /* n_P160 */
  337. tmp /= 2;
  338. *pri160 = c->center_freq1 - 80 + 160 * tmp;
  339. break;
  340. default:
  341. WARN_ON_ONCE(1);
  342. }
  343. }
  344. const struct cfg80211_chan_def *
  345. cfg80211_chandef_compatible(const struct cfg80211_chan_def *c1,
  346. const struct cfg80211_chan_def *c2)
  347. {
  348. u32 c1_pri40, c1_pri80, c2_pri40, c2_pri80, c1_pri160, c2_pri160;
  349. /* If they are identical, return */
  350. if (cfg80211_chandef_identical(c1, c2))
  351. return c1;
  352. /* otherwise, must have same control channel */
  353. if (c1->chan != c2->chan)
  354. return NULL;
  355. /*
  356. * If they have the same width, but aren't identical,
  357. * then they can't be compatible.
  358. */
  359. if (c1->width == c2->width)
  360. return NULL;
  361. /*
  362. * can't be compatible if one of them is 5 or 10 MHz,
  363. * but they don't have the same width.
  364. */
  365. if (c1->width == NL80211_CHAN_WIDTH_5 ||
  366. c1->width == NL80211_CHAN_WIDTH_10 ||
  367. c2->width == NL80211_CHAN_WIDTH_5 ||
  368. c2->width == NL80211_CHAN_WIDTH_10)
  369. return NULL;
  370. if (c1->width == NL80211_CHAN_WIDTH_20_NOHT ||
  371. c1->width == NL80211_CHAN_WIDTH_20)
  372. return c2;
  373. if (c2->width == NL80211_CHAN_WIDTH_20_NOHT ||
  374. c2->width == NL80211_CHAN_WIDTH_20)
  375. return c1;
  376. chandef_primary_freqs(c1, &c1_pri40, &c1_pri80, &c1_pri160);
  377. chandef_primary_freqs(c2, &c2_pri40, &c2_pri80, &c2_pri160);
  378. if (c1_pri40 != c2_pri40)
  379. return NULL;
  380. if (c1->width == NL80211_CHAN_WIDTH_40)
  381. return c2;
  382. if (c2->width == NL80211_CHAN_WIDTH_40)
  383. return c1;
  384. if (c1_pri80 != c2_pri80)
  385. return NULL;
  386. if (c1->width == NL80211_CHAN_WIDTH_80 &&
  387. c2->width > NL80211_CHAN_WIDTH_80)
  388. return c2;
  389. if (c2->width == NL80211_CHAN_WIDTH_80 &&
  390. c1->width > NL80211_CHAN_WIDTH_80)
  391. return c1;
  392. WARN_ON(!c1_pri160 && !c2_pri160);
  393. if (c1_pri160 && c2_pri160 && c1_pri160 != c2_pri160)
  394. return NULL;
  395. if (c1->width > c2->width)
  396. return c1;
  397. return c2;
  398. }
  399. EXPORT_SYMBOL(cfg80211_chandef_compatible);
  400. static void cfg80211_set_chans_dfs_state(struct wiphy *wiphy, u32 center_freq,
  401. u32 bandwidth,
  402. enum nl80211_dfs_state dfs_state)
  403. {
  404. struct ieee80211_channel *c;
  405. u32 freq;
  406. for (freq = center_freq - bandwidth/2 + 10;
  407. freq <= center_freq + bandwidth/2 - 10;
  408. freq += 20) {
  409. c = ieee80211_get_channel(wiphy, freq);
  410. if (!c || !(c->flags & IEEE80211_CHAN_RADAR))
  411. continue;
  412. c->dfs_state = dfs_state;
  413. c->dfs_state_entered = jiffies;
  414. }
  415. }
  416. void cfg80211_set_dfs_state(struct wiphy *wiphy,
  417. const struct cfg80211_chan_def *chandef,
  418. enum nl80211_dfs_state dfs_state)
  419. {
  420. int width;
  421. if (WARN_ON(!cfg80211_chandef_valid(chandef)))
  422. return;
  423. width = cfg80211_chandef_get_width(chandef);
  424. if (width < 0)
  425. return;
  426. cfg80211_set_chans_dfs_state(wiphy, chandef->center_freq1,
  427. width, dfs_state);
  428. if (!chandef->center_freq2)
  429. return;
  430. cfg80211_set_chans_dfs_state(wiphy, chandef->center_freq2,
  431. width, dfs_state);
  432. }
  433. static u32 cfg80211_get_start_freq(u32 center_freq,
  434. u32 bandwidth)
  435. {
  436. u32 start_freq;
  437. bandwidth = MHZ_TO_KHZ(bandwidth);
  438. if (bandwidth <= MHZ_TO_KHZ(20))
  439. start_freq = center_freq;
  440. else
  441. start_freq = center_freq - bandwidth / 2 + MHZ_TO_KHZ(10);
  442. return start_freq;
  443. }
  444. static u32 cfg80211_get_end_freq(u32 center_freq,
  445. u32 bandwidth)
  446. {
  447. u32 end_freq;
  448. bandwidth = MHZ_TO_KHZ(bandwidth);
  449. if (bandwidth <= MHZ_TO_KHZ(20))
  450. end_freq = center_freq;
  451. else
  452. end_freq = center_freq + bandwidth / 2 - MHZ_TO_KHZ(10);
  453. return end_freq;
  454. }
  455. static int cfg80211_get_chans_dfs_required(struct wiphy *wiphy,
  456. u32 center_freq,
  457. u32 bandwidth)
  458. {
  459. struct ieee80211_channel *c;
  460. u32 freq, start_freq, end_freq;
  461. start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
  462. end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
  463. for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
  464. c = ieee80211_get_channel_khz(wiphy, freq);
  465. if (!c)
  466. return -EINVAL;
  467. if (c->flags & IEEE80211_CHAN_RADAR)
  468. return 1;
  469. }
  470. return 0;
  471. }
  472. int cfg80211_chandef_dfs_required(struct wiphy *wiphy,
  473. const struct cfg80211_chan_def *chandef,
  474. enum nl80211_iftype iftype)
  475. {
  476. int width;
  477. int ret;
  478. if (WARN_ON(!cfg80211_chandef_valid(chandef)))
  479. return -EINVAL;
  480. switch (iftype) {
  481. case NL80211_IFTYPE_ADHOC:
  482. case NL80211_IFTYPE_AP:
  483. case NL80211_IFTYPE_P2P_GO:
  484. case NL80211_IFTYPE_MESH_POINT:
  485. width = cfg80211_chandef_get_width(chandef);
  486. if (width < 0)
  487. return -EINVAL;
  488. ret = cfg80211_get_chans_dfs_required(wiphy,
  489. ieee80211_chandef_to_khz(chandef),
  490. width);
  491. if (ret < 0)
  492. return ret;
  493. else if (ret > 0)
  494. return BIT(chandef->width);
  495. if (!chandef->center_freq2)
  496. return 0;
  497. ret = cfg80211_get_chans_dfs_required(wiphy,
  498. MHZ_TO_KHZ(chandef->center_freq2),
  499. width);
  500. if (ret < 0)
  501. return ret;
  502. else if (ret > 0)
  503. return BIT(chandef->width);
  504. break;
  505. case NL80211_IFTYPE_STATION:
  506. case NL80211_IFTYPE_OCB:
  507. case NL80211_IFTYPE_P2P_CLIENT:
  508. case NL80211_IFTYPE_MONITOR:
  509. case NL80211_IFTYPE_AP_VLAN:
  510. case NL80211_IFTYPE_P2P_DEVICE:
  511. case NL80211_IFTYPE_NAN:
  512. break;
  513. case NL80211_IFTYPE_WDS:
  514. case NL80211_IFTYPE_UNSPECIFIED:
  515. case NUM_NL80211_IFTYPES:
  516. WARN_ON(1);
  517. }
  518. return 0;
  519. }
  520. EXPORT_SYMBOL(cfg80211_chandef_dfs_required);
  521. static int cfg80211_get_chans_dfs_usable(struct wiphy *wiphy,
  522. u32 center_freq,
  523. u32 bandwidth)
  524. {
  525. struct ieee80211_channel *c;
  526. u32 freq, start_freq, end_freq;
  527. int count = 0;
  528. start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
  529. end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
  530. /*
  531. * Check entire range of channels for the bandwidth.
  532. * Check all channels are DFS channels (DFS_USABLE or
  533. * DFS_AVAILABLE). Return number of usable channels
  534. * (require CAC). Allow DFS and non-DFS channel mix.
  535. */
  536. for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
  537. c = ieee80211_get_channel_khz(wiphy, freq);
  538. if (!c)
  539. return -EINVAL;
  540. if (c->flags & IEEE80211_CHAN_DISABLED)
  541. return -EINVAL;
  542. if (c->flags & IEEE80211_CHAN_RADAR) {
  543. if (c->dfs_state == NL80211_DFS_UNAVAILABLE)
  544. return -EINVAL;
  545. if (c->dfs_state == NL80211_DFS_USABLE)
  546. count++;
  547. }
  548. }
  549. return count;
  550. }
  551. bool cfg80211_chandef_dfs_usable(struct wiphy *wiphy,
  552. const struct cfg80211_chan_def *chandef)
  553. {
  554. int width;
  555. int r1, r2 = 0;
  556. if (WARN_ON(!cfg80211_chandef_valid(chandef)))
  557. return false;
  558. width = cfg80211_chandef_get_width(chandef);
  559. if (width < 0)
  560. return false;
  561. r1 = cfg80211_get_chans_dfs_usable(wiphy,
  562. MHZ_TO_KHZ(chandef->center_freq1),
  563. width);
  564. if (r1 < 0)
  565. return false;
  566. switch (chandef->width) {
  567. case NL80211_CHAN_WIDTH_80P80:
  568. WARN_ON(!chandef->center_freq2);
  569. r2 = cfg80211_get_chans_dfs_usable(wiphy,
  570. MHZ_TO_KHZ(chandef->center_freq2),
  571. width);
  572. if (r2 < 0)
  573. return false;
  574. break;
  575. default:
  576. WARN_ON(chandef->center_freq2);
  577. break;
  578. }
  579. return (r1 + r2 > 0);
  580. }
  581. /*
  582. * Checks if center frequency of chan falls with in the bandwidth
  583. * range of chandef.
  584. */
  585. bool cfg80211_is_sub_chan(struct cfg80211_chan_def *chandef,
  586. struct ieee80211_channel *chan,
  587. bool primary_only)
  588. {
  589. int width;
  590. u32 freq;
  591. if (!chandef->chan)
  592. return false;
  593. if (chandef->chan->center_freq == chan->center_freq)
  594. return true;
  595. if (primary_only)
  596. return false;
  597. width = cfg80211_chandef_get_width(chandef);
  598. if (width <= 20)
  599. return false;
  600. for (freq = chandef->center_freq1 - width / 2 + 10;
  601. freq <= chandef->center_freq1 + width / 2 - 10; freq += 20) {
  602. if (chan->center_freq == freq)
  603. return true;
  604. }
  605. if (!chandef->center_freq2)
  606. return false;
  607. for (freq = chandef->center_freq2 - width / 2 + 10;
  608. freq <= chandef->center_freq2 + width / 2 - 10; freq += 20) {
  609. if (chan->center_freq == freq)
  610. return true;
  611. }
  612. return false;
  613. }
  614. bool cfg80211_beaconing_iface_active(struct wireless_dev *wdev)
  615. {
  616. unsigned int link;
  617. ASSERT_WDEV_LOCK(wdev);
  618. switch (wdev->iftype) {
  619. case NL80211_IFTYPE_AP:
  620. case NL80211_IFTYPE_P2P_GO:
  621. for_each_valid_link(wdev, link) {
  622. if (wdev->links[link].ap.beacon_interval)
  623. return true;
  624. }
  625. break;
  626. case NL80211_IFTYPE_ADHOC:
  627. if (wdev->u.ibss.ssid_len)
  628. return true;
  629. break;
  630. case NL80211_IFTYPE_MESH_POINT:
  631. if (wdev->u.mesh.id_len)
  632. return true;
  633. break;
  634. case NL80211_IFTYPE_STATION:
  635. case NL80211_IFTYPE_OCB:
  636. case NL80211_IFTYPE_P2P_CLIENT:
  637. case NL80211_IFTYPE_MONITOR:
  638. case NL80211_IFTYPE_AP_VLAN:
  639. case NL80211_IFTYPE_P2P_DEVICE:
  640. /* Can NAN type be considered as beaconing interface? */
  641. case NL80211_IFTYPE_NAN:
  642. break;
  643. case NL80211_IFTYPE_UNSPECIFIED:
  644. case NL80211_IFTYPE_WDS:
  645. case NUM_NL80211_IFTYPES:
  646. WARN_ON(1);
  647. }
  648. return false;
  649. }
  650. bool cfg80211_wdev_on_sub_chan(struct wireless_dev *wdev,
  651. struct ieee80211_channel *chan,
  652. bool primary_only)
  653. {
  654. unsigned int link;
  655. switch (wdev->iftype) {
  656. case NL80211_IFTYPE_AP:
  657. case NL80211_IFTYPE_P2P_GO:
  658. for_each_valid_link(wdev, link) {
  659. if (cfg80211_is_sub_chan(&wdev->links[link].ap.chandef,
  660. chan, primary_only))
  661. return true;
  662. }
  663. break;
  664. case NL80211_IFTYPE_ADHOC:
  665. return cfg80211_is_sub_chan(&wdev->u.ibss.chandef, chan,
  666. primary_only);
  667. case NL80211_IFTYPE_MESH_POINT:
  668. return cfg80211_is_sub_chan(&wdev->u.mesh.chandef, chan,
  669. primary_only);
  670. default:
  671. break;
  672. }
  673. return false;
  674. }
  675. static bool cfg80211_is_wiphy_oper_chan(struct wiphy *wiphy,
  676. struct ieee80211_channel *chan)
  677. {
  678. struct wireless_dev *wdev;
  679. list_for_each_entry(wdev, &wiphy->wdev_list, list) {
  680. wdev_lock(wdev);
  681. if (!cfg80211_beaconing_iface_active(wdev)) {
  682. wdev_unlock(wdev);
  683. continue;
  684. }
  685. if (cfg80211_wdev_on_sub_chan(wdev, chan, false)) {
  686. wdev_unlock(wdev);
  687. return true;
  688. }
  689. wdev_unlock(wdev);
  690. }
  691. return false;
  692. }
  693. static bool
  694. cfg80211_offchan_chain_is_active(struct cfg80211_registered_device *rdev,
  695. struct ieee80211_channel *channel)
  696. {
  697. if (!rdev->background_radar_wdev)
  698. return false;
  699. if (!cfg80211_chandef_valid(&rdev->background_radar_chandef))
  700. return false;
  701. return cfg80211_is_sub_chan(&rdev->background_radar_chandef, channel,
  702. false);
  703. }
  704. bool cfg80211_any_wiphy_oper_chan(struct wiphy *wiphy,
  705. struct ieee80211_channel *chan)
  706. {
  707. struct cfg80211_registered_device *rdev;
  708. ASSERT_RTNL();
  709. if (!(chan->flags & IEEE80211_CHAN_RADAR))
  710. return false;
  711. list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
  712. if (!reg_dfs_domain_same(wiphy, &rdev->wiphy))
  713. continue;
  714. if (cfg80211_is_wiphy_oper_chan(&rdev->wiphy, chan))
  715. return true;
  716. if (cfg80211_offchan_chain_is_active(rdev, chan))
  717. return true;
  718. }
  719. return false;
  720. }
  721. static bool cfg80211_get_chans_dfs_available(struct wiphy *wiphy,
  722. u32 center_freq,
  723. u32 bandwidth)
  724. {
  725. struct ieee80211_channel *c;
  726. u32 freq, start_freq, end_freq;
  727. bool dfs_offload;
  728. dfs_offload = wiphy_ext_feature_isset(wiphy,
  729. NL80211_EXT_FEATURE_DFS_OFFLOAD);
  730. start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
  731. end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
  732. /*
  733. * Check entire range of channels for the bandwidth.
  734. * If any channel in between is disabled or has not
  735. * had gone through CAC return false
  736. */
  737. for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
  738. c = ieee80211_get_channel_khz(wiphy, freq);
  739. if (!c)
  740. return false;
  741. if (c->flags & IEEE80211_CHAN_DISABLED)
  742. return false;
  743. if ((c->flags & IEEE80211_CHAN_RADAR) &&
  744. (c->dfs_state != NL80211_DFS_AVAILABLE) &&
  745. !(c->dfs_state == NL80211_DFS_USABLE && dfs_offload))
  746. return false;
  747. }
  748. return true;
  749. }
  750. static bool cfg80211_chandef_dfs_available(struct wiphy *wiphy,
  751. const struct cfg80211_chan_def *chandef)
  752. {
  753. int width;
  754. int r;
  755. if (WARN_ON(!cfg80211_chandef_valid(chandef)))
  756. return false;
  757. width = cfg80211_chandef_get_width(chandef);
  758. if (width < 0)
  759. return false;
  760. r = cfg80211_get_chans_dfs_available(wiphy,
  761. MHZ_TO_KHZ(chandef->center_freq1),
  762. width);
  763. /* If any of channels unavailable for cf1 just return */
  764. if (!r)
  765. return r;
  766. switch (chandef->width) {
  767. case NL80211_CHAN_WIDTH_80P80:
  768. WARN_ON(!chandef->center_freq2);
  769. r = cfg80211_get_chans_dfs_available(wiphy,
  770. MHZ_TO_KHZ(chandef->center_freq2),
  771. width);
  772. break;
  773. default:
  774. WARN_ON(chandef->center_freq2);
  775. break;
  776. }
  777. return r;
  778. }
  779. static unsigned int cfg80211_get_chans_dfs_cac_time(struct wiphy *wiphy,
  780. u32 center_freq,
  781. u32 bandwidth)
  782. {
  783. struct ieee80211_channel *c;
  784. u32 start_freq, end_freq, freq;
  785. unsigned int dfs_cac_ms = 0;
  786. start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
  787. end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
  788. for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
  789. c = ieee80211_get_channel_khz(wiphy, freq);
  790. if (!c)
  791. return 0;
  792. if (c->flags & IEEE80211_CHAN_DISABLED)
  793. return 0;
  794. if (!(c->flags & IEEE80211_CHAN_RADAR))
  795. continue;
  796. if (c->dfs_cac_ms > dfs_cac_ms)
  797. dfs_cac_ms = c->dfs_cac_ms;
  798. }
  799. return dfs_cac_ms;
  800. }
  801. unsigned int
  802. cfg80211_chandef_dfs_cac_time(struct wiphy *wiphy,
  803. const struct cfg80211_chan_def *chandef)
  804. {
  805. int width;
  806. unsigned int t1 = 0, t2 = 0;
  807. if (WARN_ON(!cfg80211_chandef_valid(chandef)))
  808. return 0;
  809. width = cfg80211_chandef_get_width(chandef);
  810. if (width < 0)
  811. return 0;
  812. t1 = cfg80211_get_chans_dfs_cac_time(wiphy,
  813. MHZ_TO_KHZ(chandef->center_freq1),
  814. width);
  815. if (!chandef->center_freq2)
  816. return t1;
  817. t2 = cfg80211_get_chans_dfs_cac_time(wiphy,
  818. MHZ_TO_KHZ(chandef->center_freq2),
  819. width);
  820. return max(t1, t2);
  821. }
  822. static bool cfg80211_secondary_chans_ok(struct wiphy *wiphy,
  823. u32 center_freq, u32 bandwidth,
  824. u32 prohibited_flags)
  825. {
  826. struct ieee80211_channel *c;
  827. u32 freq, start_freq, end_freq;
  828. start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
  829. end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
  830. for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
  831. c = ieee80211_get_channel_khz(wiphy, freq);
  832. if (!c || c->flags & prohibited_flags)
  833. return false;
  834. }
  835. return true;
  836. }
  837. /* check if the operating channels are valid and supported */
  838. static bool cfg80211_edmg_usable(struct wiphy *wiphy, u8 edmg_channels,
  839. enum ieee80211_edmg_bw_config edmg_bw_config,
  840. int primary_channel,
  841. struct ieee80211_edmg *edmg_cap)
  842. {
  843. struct ieee80211_channel *chan;
  844. int i, freq;
  845. int channels_counter = 0;
  846. if (!edmg_channels && !edmg_bw_config)
  847. return true;
  848. if ((!edmg_channels && edmg_bw_config) ||
  849. (edmg_channels && !edmg_bw_config))
  850. return false;
  851. if (!(edmg_channels & BIT(primary_channel - 1)))
  852. return false;
  853. /* 60GHz channels 1..6 */
  854. for (i = 0; i < 6; i++) {
  855. if (!(edmg_channels & BIT(i)))
  856. continue;
  857. if (!(edmg_cap->channels & BIT(i)))
  858. return false;
  859. channels_counter++;
  860. freq = ieee80211_channel_to_frequency(i + 1,
  861. NL80211_BAND_60GHZ);
  862. chan = ieee80211_get_channel(wiphy, freq);
  863. if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
  864. return false;
  865. }
  866. /* IEEE802.11 allows max 4 channels */
  867. if (channels_counter > 4)
  868. return false;
  869. /* check bw_config is a subset of what driver supports
  870. * (see IEEE P802.11ay/D4.0 section 9.4.2.251, Table 13)
  871. */
  872. if ((edmg_bw_config % 4) > (edmg_cap->bw_config % 4))
  873. return false;
  874. if (edmg_bw_config > edmg_cap->bw_config)
  875. return false;
  876. return true;
  877. }
  878. bool cfg80211_chandef_usable(struct wiphy *wiphy,
  879. const struct cfg80211_chan_def *chandef,
  880. u32 prohibited_flags)
  881. {
  882. struct ieee80211_sta_ht_cap *ht_cap;
  883. struct ieee80211_sta_vht_cap *vht_cap;
  884. struct ieee80211_edmg *edmg_cap;
  885. u32 width, control_freq, cap;
  886. bool ext_nss_cap, support_80_80 = false, support_320 = false;
  887. const struct ieee80211_sband_iftype_data *iftd;
  888. struct ieee80211_supported_band *sband;
  889. int i;
  890. if (WARN_ON(!cfg80211_chandef_valid(chandef)))
  891. return false;
  892. ht_cap = &wiphy->bands[chandef->chan->band]->ht_cap;
  893. vht_cap = &wiphy->bands[chandef->chan->band]->vht_cap;
  894. edmg_cap = &wiphy->bands[chandef->chan->band]->edmg_cap;
  895. ext_nss_cap = __le16_to_cpu(vht_cap->vht_mcs.tx_highest) &
  896. IEEE80211_VHT_EXT_NSS_BW_CAPABLE;
  897. if (edmg_cap->channels &&
  898. !cfg80211_edmg_usable(wiphy,
  899. chandef->edmg.channels,
  900. chandef->edmg.bw_config,
  901. chandef->chan->hw_value,
  902. edmg_cap))
  903. return false;
  904. control_freq = chandef->chan->center_freq;
  905. switch (chandef->width) {
  906. case NL80211_CHAN_WIDTH_1:
  907. width = 1;
  908. break;
  909. case NL80211_CHAN_WIDTH_2:
  910. width = 2;
  911. break;
  912. case NL80211_CHAN_WIDTH_4:
  913. width = 4;
  914. break;
  915. case NL80211_CHAN_WIDTH_8:
  916. width = 8;
  917. break;
  918. case NL80211_CHAN_WIDTH_16:
  919. width = 16;
  920. break;
  921. case NL80211_CHAN_WIDTH_5:
  922. width = 5;
  923. break;
  924. case NL80211_CHAN_WIDTH_10:
  925. prohibited_flags |= IEEE80211_CHAN_NO_10MHZ;
  926. width = 10;
  927. break;
  928. case NL80211_CHAN_WIDTH_20:
  929. if (!ht_cap->ht_supported &&
  930. chandef->chan->band != NL80211_BAND_6GHZ)
  931. return false;
  932. fallthrough;
  933. case NL80211_CHAN_WIDTH_20_NOHT:
  934. prohibited_flags |= IEEE80211_CHAN_NO_20MHZ;
  935. width = 20;
  936. break;
  937. case NL80211_CHAN_WIDTH_40:
  938. width = 40;
  939. if (chandef->chan->band == NL80211_BAND_6GHZ)
  940. break;
  941. if (!ht_cap->ht_supported)
  942. return false;
  943. if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
  944. ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT)
  945. return false;
  946. if (chandef->center_freq1 < control_freq &&
  947. chandef->chan->flags & IEEE80211_CHAN_NO_HT40MINUS)
  948. return false;
  949. if (chandef->center_freq1 > control_freq &&
  950. chandef->chan->flags & IEEE80211_CHAN_NO_HT40PLUS)
  951. return false;
  952. break;
  953. case NL80211_CHAN_WIDTH_80P80:
  954. cap = vht_cap->cap;
  955. support_80_80 =
  956. (cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ) ||
  957. (cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ &&
  958. cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) ||
  959. (ext_nss_cap &&
  960. u32_get_bits(cap, IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) > 1);
  961. if (chandef->chan->band != NL80211_BAND_6GHZ && !support_80_80)
  962. return false;
  963. fallthrough;
  964. case NL80211_CHAN_WIDTH_80:
  965. prohibited_flags |= IEEE80211_CHAN_NO_80MHZ;
  966. width = 80;
  967. if (chandef->chan->band == NL80211_BAND_6GHZ)
  968. break;
  969. if (!vht_cap->vht_supported)
  970. return false;
  971. break;
  972. case NL80211_CHAN_WIDTH_160:
  973. prohibited_flags |= IEEE80211_CHAN_NO_160MHZ;
  974. width = 160;
  975. if (chandef->chan->band == NL80211_BAND_6GHZ)
  976. break;
  977. if (!vht_cap->vht_supported)
  978. return false;
  979. cap = vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
  980. if (cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ &&
  981. cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ &&
  982. !(ext_nss_cap &&
  983. (vht_cap->cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK)))
  984. return false;
  985. break;
  986. case NL80211_CHAN_WIDTH_320:
  987. prohibited_flags |= IEEE80211_CHAN_NO_320MHZ;
  988. width = 320;
  989. if (chandef->chan->band != NL80211_BAND_6GHZ)
  990. return false;
  991. sband = wiphy->bands[NL80211_BAND_6GHZ];
  992. if (!sband)
  993. return false;
  994. for (i = 0; i < sband->n_iftype_data; i++) {
  995. iftd = &sband->iftype_data[i];
  996. if (!iftd->eht_cap.has_eht)
  997. continue;
  998. if (iftd->eht_cap.eht_cap_elem.phy_cap_info[0] &
  999. IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ) {
  1000. support_320 = true;
  1001. break;
  1002. }
  1003. }
  1004. if (!support_320)
  1005. return false;
  1006. break;
  1007. default:
  1008. WARN_ON_ONCE(1);
  1009. return false;
  1010. }
  1011. /*
  1012. * TODO: What if there are only certain 80/160/80+80 MHz channels
  1013. * allowed by the driver, or only certain combinations?
  1014. * For 40 MHz the driver can set the NO_HT40 flags, but for
  1015. * 80/160 MHz and in particular 80+80 MHz this isn't really
  1016. * feasible and we only have NO_80MHZ/NO_160MHZ so far but
  1017. * no way to cover 80+80 MHz or more complex restrictions.
  1018. * Note that such restrictions also need to be advertised to
  1019. * userspace, for example for P2P channel selection.
  1020. */
  1021. if (width > 20)
  1022. prohibited_flags |= IEEE80211_CHAN_NO_OFDM;
  1023. /* 5 and 10 MHz are only defined for the OFDM PHY */
  1024. if (width < 20)
  1025. prohibited_flags |= IEEE80211_CHAN_NO_OFDM;
  1026. if (!cfg80211_secondary_chans_ok(wiphy,
  1027. ieee80211_chandef_to_khz(chandef),
  1028. width, prohibited_flags))
  1029. return false;
  1030. if (!chandef->center_freq2)
  1031. return true;
  1032. return cfg80211_secondary_chans_ok(wiphy,
  1033. MHZ_TO_KHZ(chandef->center_freq2),
  1034. width, prohibited_flags);
  1035. }
  1036. EXPORT_SYMBOL(cfg80211_chandef_usable);
  1037. static bool cfg80211_ir_permissive_check_wdev(enum nl80211_iftype iftype,
  1038. struct wireless_dev *wdev,
  1039. struct ieee80211_channel *chan)
  1040. {
  1041. struct ieee80211_channel *other_chan = NULL;
  1042. unsigned int link_id;
  1043. int r1, r2;
  1044. for_each_valid_link(wdev, link_id) {
  1045. if (wdev->iftype == NL80211_IFTYPE_STATION &&
  1046. wdev->links[link_id].client.current_bss)
  1047. other_chan = wdev->links[link_id].client.current_bss->pub.channel;
  1048. /*
  1049. * If a GO already operates on the same GO_CONCURRENT channel,
  1050. * this one (maybe the same one) can beacon as well. We allow
  1051. * the operation even if the station we relied on with
  1052. * GO_CONCURRENT is disconnected now. But then we must make sure
  1053. * we're not outdoor on an indoor-only channel.
  1054. */
  1055. if (iftype == NL80211_IFTYPE_P2P_GO &&
  1056. wdev->iftype == NL80211_IFTYPE_P2P_GO &&
  1057. wdev->links[link_id].ap.beacon_interval &&
  1058. !(chan->flags & IEEE80211_CHAN_INDOOR_ONLY))
  1059. other_chan = wdev->links[link_id].ap.chandef.chan;
  1060. if (!other_chan)
  1061. continue;
  1062. if (chan == other_chan)
  1063. return true;
  1064. if (chan->band != NL80211_BAND_5GHZ &&
  1065. chan->band != NL80211_BAND_6GHZ)
  1066. continue;
  1067. r1 = cfg80211_get_unii(chan->center_freq);
  1068. r2 = cfg80211_get_unii(other_chan->center_freq);
  1069. if (r1 != -EINVAL && r1 == r2) {
  1070. /*
  1071. * At some locations channels 149-165 are considered a
  1072. * bundle, but at other locations, e.g., Indonesia,
  1073. * channels 149-161 are considered a bundle while
  1074. * channel 165 is left out and considered to be in a
  1075. * different bundle. Thus, in case that there is a
  1076. * station interface connected to an AP on channel 165,
  1077. * it is assumed that channels 149-161 are allowed for
  1078. * GO operations. However, having a station interface
  1079. * connected to an AP on channels 149-161, does not
  1080. * allow GO operation on channel 165.
  1081. */
  1082. if (chan->center_freq == 5825 &&
  1083. other_chan->center_freq != 5825)
  1084. continue;
  1085. return true;
  1086. }
  1087. }
  1088. return false;
  1089. }
  1090. /*
  1091. * Check if the channel can be used under permissive conditions mandated by
  1092. * some regulatory bodies, i.e., the channel is marked with
  1093. * IEEE80211_CHAN_IR_CONCURRENT and there is an additional station interface
  1094. * associated to an AP on the same channel or on the same UNII band
  1095. * (assuming that the AP is an authorized master).
  1096. * In addition allow operation on a channel on which indoor operation is
  1097. * allowed, iff we are currently operating in an indoor environment.
  1098. */
  1099. static bool cfg80211_ir_permissive_chan(struct wiphy *wiphy,
  1100. enum nl80211_iftype iftype,
  1101. struct ieee80211_channel *chan)
  1102. {
  1103. struct wireless_dev *wdev;
  1104. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  1105. lockdep_assert_held(&rdev->wiphy.mtx);
  1106. if (!IS_ENABLED(CONFIG_CFG80211_REG_RELAX_NO_IR) ||
  1107. !(wiphy->regulatory_flags & REGULATORY_ENABLE_RELAX_NO_IR))
  1108. return false;
  1109. /* only valid for GO and TDLS off-channel (station/p2p-CL) */
  1110. if (iftype != NL80211_IFTYPE_P2P_GO &&
  1111. iftype != NL80211_IFTYPE_STATION &&
  1112. iftype != NL80211_IFTYPE_P2P_CLIENT)
  1113. return false;
  1114. if (regulatory_indoor_allowed() &&
  1115. (chan->flags & IEEE80211_CHAN_INDOOR_ONLY))
  1116. return true;
  1117. if (!(chan->flags & IEEE80211_CHAN_IR_CONCURRENT))
  1118. return false;
  1119. /*
  1120. * Generally, it is possible to rely on another device/driver to allow
  1121. * the IR concurrent relaxation, however, since the device can further
  1122. * enforce the relaxation (by doing a similar verifications as this),
  1123. * and thus fail the GO instantiation, consider only the interfaces of
  1124. * the current registered device.
  1125. */
  1126. list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
  1127. bool ret;
  1128. wdev_lock(wdev);
  1129. ret = cfg80211_ir_permissive_check_wdev(iftype, wdev, chan);
  1130. wdev_unlock(wdev);
  1131. if (ret)
  1132. return ret;
  1133. }
  1134. return false;
  1135. }
  1136. static bool _cfg80211_reg_can_beacon(struct wiphy *wiphy,
  1137. struct cfg80211_chan_def *chandef,
  1138. enum nl80211_iftype iftype,
  1139. bool check_no_ir)
  1140. {
  1141. bool res;
  1142. u32 prohibited_flags = IEEE80211_CHAN_DISABLED |
  1143. IEEE80211_CHAN_RADAR;
  1144. trace_cfg80211_reg_can_beacon(wiphy, chandef, iftype, check_no_ir);
  1145. if (check_no_ir)
  1146. prohibited_flags |= IEEE80211_CHAN_NO_IR;
  1147. if (cfg80211_chandef_dfs_required(wiphy, chandef, iftype) > 0 &&
  1148. cfg80211_chandef_dfs_available(wiphy, chandef)) {
  1149. /* We can skip IEEE80211_CHAN_NO_IR if chandef dfs available */
  1150. prohibited_flags = IEEE80211_CHAN_DISABLED;
  1151. }
  1152. res = cfg80211_chandef_usable(wiphy, chandef, prohibited_flags);
  1153. trace_cfg80211_return_bool(res);
  1154. return res;
  1155. }
  1156. bool cfg80211_reg_can_beacon(struct wiphy *wiphy,
  1157. struct cfg80211_chan_def *chandef,
  1158. enum nl80211_iftype iftype)
  1159. {
  1160. return _cfg80211_reg_can_beacon(wiphy, chandef, iftype, true);
  1161. }
  1162. EXPORT_SYMBOL(cfg80211_reg_can_beacon);
  1163. bool cfg80211_reg_can_beacon_relax(struct wiphy *wiphy,
  1164. struct cfg80211_chan_def *chandef,
  1165. enum nl80211_iftype iftype)
  1166. {
  1167. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  1168. bool check_no_ir;
  1169. lockdep_assert_held(&rdev->wiphy.mtx);
  1170. /*
  1171. * Under certain conditions suggested by some regulatory bodies a
  1172. * GO/STA can IR on channels marked with IEEE80211_NO_IR. Set this flag
  1173. * only if such relaxations are not enabled and the conditions are not
  1174. * met.
  1175. */
  1176. check_no_ir = !cfg80211_ir_permissive_chan(wiphy, iftype,
  1177. chandef->chan);
  1178. return _cfg80211_reg_can_beacon(wiphy, chandef, iftype, check_no_ir);
  1179. }
  1180. EXPORT_SYMBOL(cfg80211_reg_can_beacon_relax);
  1181. int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev,
  1182. struct cfg80211_chan_def *chandef)
  1183. {
  1184. if (!rdev->ops->set_monitor_channel)
  1185. return -EOPNOTSUPP;
  1186. if (!cfg80211_has_monitors_only(rdev))
  1187. return -EBUSY;
  1188. return rdev_set_monitor_channel(rdev, chandef);
  1189. }
  1190. bool cfg80211_any_usable_channels(struct wiphy *wiphy,
  1191. unsigned long sband_mask,
  1192. u32 prohibited_flags)
  1193. {
  1194. int idx;
  1195. prohibited_flags |= IEEE80211_CHAN_DISABLED;
  1196. for_each_set_bit(idx, &sband_mask, NUM_NL80211_BANDS) {
  1197. struct ieee80211_supported_band *sband = wiphy->bands[idx];
  1198. int chanidx;
  1199. if (!sband)
  1200. continue;
  1201. for (chanidx = 0; chanidx < sband->n_channels; chanidx++) {
  1202. struct ieee80211_channel *chan;
  1203. chan = &sband->channels[chanidx];
  1204. if (chan->flags & prohibited_flags)
  1205. continue;
  1206. return true;
  1207. }
  1208. }
  1209. return false;
  1210. }
  1211. EXPORT_SYMBOL(cfg80211_any_usable_channels);
  1212. struct cfg80211_chan_def *wdev_chandef(struct wireless_dev *wdev,
  1213. unsigned int link_id)
  1214. {
  1215. /*
  1216. * We need to sort out the locking here - in some cases
  1217. * where we get here we really just don't care (yet)
  1218. * about the valid links, but in others we do. But we
  1219. * get here with various driver cases, so we cannot
  1220. * easily require the wdev mutex.
  1221. */
  1222. if (link_id || wdev->valid_links & BIT(0)) {
  1223. ASSERT_WDEV_LOCK(wdev);
  1224. WARN_ON(!(wdev->valid_links & BIT(link_id)));
  1225. }
  1226. switch (wdev->iftype) {
  1227. case NL80211_IFTYPE_MESH_POINT:
  1228. return &wdev->u.mesh.chandef;
  1229. case NL80211_IFTYPE_ADHOC:
  1230. return &wdev->u.ibss.chandef;
  1231. case NL80211_IFTYPE_OCB:
  1232. return &wdev->u.ocb.chandef;
  1233. case NL80211_IFTYPE_AP:
  1234. case NL80211_IFTYPE_P2P_GO:
  1235. return &wdev->links[link_id].ap.chandef;
  1236. default:
  1237. return NULL;
  1238. }
  1239. }
  1240. EXPORT_SYMBOL(wdev_chandef);
  1241. struct cfg80211_per_bw_puncturing_values {
  1242. u8 len;
  1243. const u16 *valid_values;
  1244. };
  1245. static const u16 puncturing_values_80mhz[] = {
  1246. 0x8, 0x4, 0x2, 0x1
  1247. };
  1248. static const u16 puncturing_values_160mhz[] = {
  1249. 0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1, 0xc0, 0x30, 0xc, 0x3
  1250. };
  1251. static const u16 puncturing_values_320mhz[] = {
  1252. 0xc000, 0x3000, 0xc00, 0x300, 0xc0, 0x30, 0xc, 0x3, 0xf000, 0xf00,
  1253. 0xf0, 0xf, 0xfc00, 0xf300, 0xf0c0, 0xf030, 0xf00c, 0xf003, 0xc00f,
  1254. 0x300f, 0xc0f, 0x30f, 0xcf, 0x3f
  1255. };
  1256. #define CFG80211_PER_BW_VALID_PUNCTURING_VALUES(_bw) \
  1257. { \
  1258. .len = ARRAY_SIZE(puncturing_values_ ## _bw ## mhz), \
  1259. .valid_values = puncturing_values_ ## _bw ## mhz \
  1260. }
  1261. static const struct cfg80211_per_bw_puncturing_values per_bw_puncturing[] = {
  1262. CFG80211_PER_BW_VALID_PUNCTURING_VALUES(80),
  1263. CFG80211_PER_BW_VALID_PUNCTURING_VALUES(160),
  1264. CFG80211_PER_BW_VALID_PUNCTURING_VALUES(320)
  1265. };
  1266. bool cfg80211_valid_disable_subchannel_bitmap(u16 *bitmap,
  1267. const struct cfg80211_chan_def *chandef)
  1268. {
  1269. u32 idx, i, start_freq;
  1270. switch (chandef->width) {
  1271. case NL80211_CHAN_WIDTH_80:
  1272. idx = 0;
  1273. start_freq = chandef->center_freq1 - 40;
  1274. break;
  1275. case NL80211_CHAN_WIDTH_160:
  1276. idx = 1;
  1277. start_freq = chandef->center_freq1 - 80;
  1278. break;
  1279. case NL80211_CHAN_WIDTH_320:
  1280. idx = 2;
  1281. start_freq = chandef->center_freq1 - 160;
  1282. break;
  1283. default:
  1284. *bitmap = 0;
  1285. break;
  1286. }
  1287. if (!*bitmap)
  1288. return true;
  1289. /* check if primary channel is punctured */
  1290. if (*bitmap & (u16)BIT((chandef->chan->center_freq - start_freq) / 20))
  1291. return false;
  1292. for (i = 0; i < per_bw_puncturing[idx].len; i++)
  1293. if (per_bw_puncturing[idx].valid_values[i] == *bitmap)
  1294. return true;
  1295. return false;
  1296. }
  1297. EXPORT_SYMBOL(cfg80211_valid_disable_subchannel_bitmap);