chan.c 40 KB

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