chan.c 24 KB

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