chan.c 42 KB

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