chan.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  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 <johannes@sipsolutions.net>
  7. */
  8. #include <linux/export.h>
  9. #include <net/cfg80211.h>
  10. #include "core.h"
  11. #include "rdev-ops.h"
  12. void cfg80211_chandef_create(struct cfg80211_chan_def *chandef,
  13. struct ieee80211_channel *chan,
  14. enum nl80211_channel_type chan_type)
  15. {
  16. if (WARN_ON(!chan))
  17. return;
  18. chandef->chan = chan;
  19. chandef->center_freq2 = 0;
  20. switch (chan_type) {
  21. case NL80211_CHAN_NO_HT:
  22. chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
  23. chandef->center_freq1 = chan->center_freq;
  24. break;
  25. case NL80211_CHAN_HT20:
  26. chandef->width = NL80211_CHAN_WIDTH_20;
  27. chandef->center_freq1 = chan->center_freq;
  28. break;
  29. case NL80211_CHAN_HT40PLUS:
  30. chandef->width = NL80211_CHAN_WIDTH_40;
  31. chandef->center_freq1 = chan->center_freq + 10;
  32. break;
  33. case NL80211_CHAN_HT40MINUS:
  34. chandef->width = NL80211_CHAN_WIDTH_40;
  35. chandef->center_freq1 = chan->center_freq - 10;
  36. break;
  37. default:
  38. WARN_ON(1);
  39. }
  40. }
  41. EXPORT_SYMBOL(cfg80211_chandef_create);
  42. bool cfg80211_chandef_valid(const struct cfg80211_chan_def *chandef)
  43. {
  44. u32 control_freq;
  45. if (!chandef->chan)
  46. return false;
  47. control_freq = chandef->chan->center_freq;
  48. switch (chandef->width) {
  49. case NL80211_CHAN_WIDTH_5:
  50. case NL80211_CHAN_WIDTH_10:
  51. case NL80211_CHAN_WIDTH_20:
  52. case NL80211_CHAN_WIDTH_20_NOHT:
  53. if (chandef->center_freq1 != control_freq)
  54. return false;
  55. if (chandef->center_freq2)
  56. return false;
  57. break;
  58. case NL80211_CHAN_WIDTH_40:
  59. if (chandef->center_freq1 != control_freq + 10 &&
  60. chandef->center_freq1 != control_freq - 10)
  61. return false;
  62. if (chandef->center_freq2)
  63. return false;
  64. break;
  65. case NL80211_CHAN_WIDTH_80P80:
  66. if (chandef->center_freq1 != control_freq + 30 &&
  67. chandef->center_freq1 != control_freq + 10 &&
  68. chandef->center_freq1 != control_freq - 10 &&
  69. chandef->center_freq1 != control_freq - 30)
  70. return false;
  71. if (!chandef->center_freq2)
  72. return false;
  73. /* adjacent is not allowed -- that's a 160 MHz channel */
  74. if (chandef->center_freq1 - chandef->center_freq2 == 80 ||
  75. chandef->center_freq2 - chandef->center_freq1 == 80)
  76. return false;
  77. break;
  78. case NL80211_CHAN_WIDTH_80:
  79. if (chandef->center_freq1 != control_freq + 30 &&
  80. chandef->center_freq1 != control_freq + 10 &&
  81. chandef->center_freq1 != control_freq - 10 &&
  82. chandef->center_freq1 != control_freq - 30)
  83. return false;
  84. if (chandef->center_freq2)
  85. return false;
  86. break;
  87. case NL80211_CHAN_WIDTH_160:
  88. if (chandef->center_freq1 != control_freq + 70 &&
  89. chandef->center_freq1 != control_freq + 50 &&
  90. chandef->center_freq1 != control_freq + 30 &&
  91. chandef->center_freq1 != control_freq + 10 &&
  92. chandef->center_freq1 != control_freq - 10 &&
  93. chandef->center_freq1 != control_freq - 30 &&
  94. chandef->center_freq1 != control_freq - 50 &&
  95. chandef->center_freq1 != control_freq - 70)
  96. return false;
  97. if (chandef->center_freq2)
  98. return false;
  99. break;
  100. default:
  101. return false;
  102. }
  103. return true;
  104. }
  105. EXPORT_SYMBOL(cfg80211_chandef_valid);
  106. static void chandef_primary_freqs(const struct cfg80211_chan_def *c,
  107. int *pri40, int *pri80)
  108. {
  109. int tmp;
  110. switch (c->width) {
  111. case NL80211_CHAN_WIDTH_40:
  112. *pri40 = c->center_freq1;
  113. *pri80 = 0;
  114. break;
  115. case NL80211_CHAN_WIDTH_80:
  116. case NL80211_CHAN_WIDTH_80P80:
  117. *pri80 = c->center_freq1;
  118. /* n_P20 */
  119. tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
  120. /* n_P40 */
  121. tmp /= 2;
  122. /* freq_P40 */
  123. *pri40 = c->center_freq1 - 20 + 40 * tmp;
  124. break;
  125. case NL80211_CHAN_WIDTH_160:
  126. /* n_P20 */
  127. tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
  128. /* n_P40 */
  129. tmp /= 2;
  130. /* freq_P40 */
  131. *pri40 = c->center_freq1 - 60 + 40 * tmp;
  132. /* n_P80 */
  133. tmp /= 2;
  134. *pri80 = c->center_freq1 - 40 + 80 * tmp;
  135. break;
  136. default:
  137. WARN_ON_ONCE(1);
  138. }
  139. }
  140. static int cfg80211_chandef_get_width(const struct cfg80211_chan_def *c)
  141. {
  142. int width;
  143. switch (c->width) {
  144. case NL80211_CHAN_WIDTH_5:
  145. width = 5;
  146. break;
  147. case NL80211_CHAN_WIDTH_10:
  148. width = 10;
  149. break;
  150. case NL80211_CHAN_WIDTH_20:
  151. case NL80211_CHAN_WIDTH_20_NOHT:
  152. width = 20;
  153. break;
  154. case NL80211_CHAN_WIDTH_40:
  155. width = 40;
  156. break;
  157. case NL80211_CHAN_WIDTH_80P80:
  158. case NL80211_CHAN_WIDTH_80:
  159. width = 80;
  160. break;
  161. case NL80211_CHAN_WIDTH_160:
  162. width = 160;
  163. break;
  164. default:
  165. WARN_ON_ONCE(1);
  166. return -1;
  167. }
  168. return width;
  169. }
  170. const struct cfg80211_chan_def *
  171. cfg80211_chandef_compatible(const struct cfg80211_chan_def *c1,
  172. const struct cfg80211_chan_def *c2)
  173. {
  174. u32 c1_pri40, c1_pri80, c2_pri40, c2_pri80;
  175. /* If they are identical, return */
  176. if (cfg80211_chandef_identical(c1, c2))
  177. return c1;
  178. /* otherwise, must have same control channel */
  179. if (c1->chan != c2->chan)
  180. return NULL;
  181. /*
  182. * If they have the same width, but aren't identical,
  183. * then they can't be compatible.
  184. */
  185. if (c1->width == c2->width)
  186. return NULL;
  187. /*
  188. * can't be compatible if one of them is 5 or 10 MHz,
  189. * but they don't have the same width.
  190. */
  191. if (c1->width == NL80211_CHAN_WIDTH_5 ||
  192. c1->width == NL80211_CHAN_WIDTH_10 ||
  193. c2->width == NL80211_CHAN_WIDTH_5 ||
  194. c2->width == NL80211_CHAN_WIDTH_10)
  195. return NULL;
  196. if (c1->width == NL80211_CHAN_WIDTH_20_NOHT ||
  197. c1->width == NL80211_CHAN_WIDTH_20)
  198. return c2;
  199. if (c2->width == NL80211_CHAN_WIDTH_20_NOHT ||
  200. c2->width == NL80211_CHAN_WIDTH_20)
  201. return c1;
  202. chandef_primary_freqs(c1, &c1_pri40, &c1_pri80);
  203. chandef_primary_freqs(c2, &c2_pri40, &c2_pri80);
  204. if (c1_pri40 != c2_pri40)
  205. return NULL;
  206. WARN_ON(!c1_pri80 && !c2_pri80);
  207. if (c1_pri80 && c2_pri80 && c1_pri80 != c2_pri80)
  208. return NULL;
  209. if (c1->width > c2->width)
  210. return c1;
  211. return c2;
  212. }
  213. EXPORT_SYMBOL(cfg80211_chandef_compatible);
  214. static void cfg80211_set_chans_dfs_state(struct wiphy *wiphy, u32 center_freq,
  215. u32 bandwidth,
  216. enum nl80211_dfs_state dfs_state)
  217. {
  218. struct ieee80211_channel *c;
  219. u32 freq;
  220. for (freq = center_freq - bandwidth/2 + 10;
  221. freq <= center_freq + bandwidth/2 - 10;
  222. freq += 20) {
  223. c = ieee80211_get_channel(wiphy, freq);
  224. if (!c || !(c->flags & IEEE80211_CHAN_RADAR))
  225. continue;
  226. c->dfs_state = dfs_state;
  227. c->dfs_state_entered = jiffies;
  228. }
  229. }
  230. void cfg80211_set_dfs_state(struct wiphy *wiphy,
  231. const struct cfg80211_chan_def *chandef,
  232. enum nl80211_dfs_state dfs_state)
  233. {
  234. int width;
  235. if (WARN_ON(!cfg80211_chandef_valid(chandef)))
  236. return;
  237. width = cfg80211_chandef_get_width(chandef);
  238. if (width < 0)
  239. return;
  240. cfg80211_set_chans_dfs_state(wiphy, chandef->center_freq1,
  241. width, dfs_state);
  242. if (!chandef->center_freq2)
  243. return;
  244. cfg80211_set_chans_dfs_state(wiphy, chandef->center_freq2,
  245. width, dfs_state);
  246. }
  247. static u32 cfg80211_get_start_freq(u32 center_freq,
  248. u32 bandwidth)
  249. {
  250. u32 start_freq;
  251. if (bandwidth <= 20)
  252. start_freq = center_freq;
  253. else
  254. start_freq = center_freq - bandwidth/2 + 10;
  255. return start_freq;
  256. }
  257. static u32 cfg80211_get_end_freq(u32 center_freq,
  258. u32 bandwidth)
  259. {
  260. u32 end_freq;
  261. if (bandwidth <= 20)
  262. end_freq = center_freq;
  263. else
  264. end_freq = center_freq + bandwidth/2 - 10;
  265. return end_freq;
  266. }
  267. static int cfg80211_get_chans_dfs_required(struct wiphy *wiphy,
  268. u32 center_freq,
  269. u32 bandwidth)
  270. {
  271. struct ieee80211_channel *c;
  272. u32 freq, start_freq, end_freq;
  273. start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
  274. end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
  275. for (freq = start_freq; freq <= end_freq; freq += 20) {
  276. c = ieee80211_get_channel(wiphy, freq);
  277. if (!c)
  278. return -EINVAL;
  279. if (c->flags & IEEE80211_CHAN_RADAR)
  280. return 1;
  281. }
  282. return 0;
  283. }
  284. int cfg80211_chandef_dfs_required(struct wiphy *wiphy,
  285. const struct cfg80211_chan_def *chandef)
  286. {
  287. int width;
  288. int r;
  289. if (WARN_ON(!cfg80211_chandef_valid(chandef)))
  290. return -EINVAL;
  291. width = cfg80211_chandef_get_width(chandef);
  292. if (width < 0)
  293. return -EINVAL;
  294. r = cfg80211_get_chans_dfs_required(wiphy, chandef->center_freq1,
  295. width);
  296. if (r)
  297. return r;
  298. if (!chandef->center_freq2)
  299. return 0;
  300. return cfg80211_get_chans_dfs_required(wiphy, chandef->center_freq2,
  301. width);
  302. }
  303. EXPORT_SYMBOL(cfg80211_chandef_dfs_required);
  304. static int cfg80211_get_chans_dfs_usable(struct wiphy *wiphy,
  305. u32 center_freq,
  306. u32 bandwidth)
  307. {
  308. struct ieee80211_channel *c;
  309. u32 freq, start_freq, end_freq;
  310. int count = 0;
  311. start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
  312. end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
  313. /*
  314. * Check entire range of channels for the bandwidth.
  315. * Check all channels are DFS channels (DFS_USABLE or
  316. * DFS_AVAILABLE). Return number of usable channels
  317. * (require CAC). Allow DFS and non-DFS channel mix.
  318. */
  319. for (freq = start_freq; freq <= end_freq; freq += 20) {
  320. c = ieee80211_get_channel(wiphy, freq);
  321. if (!c)
  322. return -EINVAL;
  323. if (c->flags & IEEE80211_CHAN_DISABLED)
  324. return -EINVAL;
  325. if (c->flags & IEEE80211_CHAN_RADAR) {
  326. if (c->dfs_state == NL80211_DFS_UNAVAILABLE)
  327. return -EINVAL;
  328. if (c->dfs_state == NL80211_DFS_USABLE)
  329. count++;
  330. }
  331. }
  332. return count;
  333. }
  334. bool cfg80211_chandef_dfs_usable(struct wiphy *wiphy,
  335. const struct cfg80211_chan_def *chandef)
  336. {
  337. int width;
  338. int r1, r2 = 0;
  339. if (WARN_ON(!cfg80211_chandef_valid(chandef)))
  340. return false;
  341. width = cfg80211_chandef_get_width(chandef);
  342. if (width < 0)
  343. return false;
  344. r1 = cfg80211_get_chans_dfs_usable(wiphy, chandef->center_freq1,
  345. width);
  346. if (r1 < 0)
  347. return false;
  348. switch (chandef->width) {
  349. case NL80211_CHAN_WIDTH_80P80:
  350. WARN_ON(!chandef->center_freq2);
  351. r2 = cfg80211_get_chans_dfs_usable(wiphy,
  352. chandef->center_freq2,
  353. width);
  354. if (r2 < 0)
  355. return false;
  356. break;
  357. default:
  358. WARN_ON(chandef->center_freq2);
  359. break;
  360. }
  361. return (r1 + r2 > 0);
  362. }
  363. static bool cfg80211_get_chans_dfs_available(struct wiphy *wiphy,
  364. u32 center_freq,
  365. u32 bandwidth)
  366. {
  367. struct ieee80211_channel *c;
  368. u32 freq, start_freq, end_freq;
  369. start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
  370. end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
  371. /*
  372. * Check entire range of channels for the bandwidth.
  373. * If any channel in between is disabled or has not
  374. * had gone through CAC return false
  375. */
  376. for (freq = start_freq; freq <= end_freq; freq += 20) {
  377. c = ieee80211_get_channel(wiphy, freq);
  378. if (!c)
  379. return false;
  380. if (c->flags & IEEE80211_CHAN_DISABLED)
  381. return false;
  382. if ((c->flags & IEEE80211_CHAN_RADAR) &&
  383. (c->dfs_state != NL80211_DFS_AVAILABLE))
  384. return false;
  385. }
  386. return true;
  387. }
  388. static bool cfg80211_chandef_dfs_available(struct wiphy *wiphy,
  389. const struct cfg80211_chan_def *chandef)
  390. {
  391. int width;
  392. int r;
  393. if (WARN_ON(!cfg80211_chandef_valid(chandef)))
  394. return false;
  395. width = cfg80211_chandef_get_width(chandef);
  396. if (width < 0)
  397. return false;
  398. r = cfg80211_get_chans_dfs_available(wiphy, chandef->center_freq1,
  399. width);
  400. /* If any of channels unavailable for cf1 just return */
  401. if (!r)
  402. return r;
  403. switch (chandef->width) {
  404. case NL80211_CHAN_WIDTH_80P80:
  405. WARN_ON(!chandef->center_freq2);
  406. r = cfg80211_get_chans_dfs_available(wiphy,
  407. chandef->center_freq2,
  408. width);
  409. default:
  410. WARN_ON(chandef->center_freq2);
  411. break;
  412. }
  413. return r;
  414. }
  415. static unsigned int cfg80211_get_chans_dfs_cac_time(struct wiphy *wiphy,
  416. u32 center_freq,
  417. u32 bandwidth)
  418. {
  419. struct ieee80211_channel *c;
  420. u32 start_freq, end_freq, freq;
  421. unsigned int dfs_cac_ms = 0;
  422. start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
  423. end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
  424. for (freq = start_freq; freq <= end_freq; freq += 20) {
  425. c = ieee80211_get_channel(wiphy, freq);
  426. if (!c)
  427. return 0;
  428. if (c->flags & IEEE80211_CHAN_DISABLED)
  429. return 0;
  430. if (!(c->flags & IEEE80211_CHAN_RADAR))
  431. continue;
  432. if (c->dfs_cac_ms > dfs_cac_ms)
  433. dfs_cac_ms = c->dfs_cac_ms;
  434. }
  435. return dfs_cac_ms;
  436. }
  437. unsigned int
  438. cfg80211_chandef_dfs_cac_time(struct wiphy *wiphy,
  439. const struct cfg80211_chan_def *chandef)
  440. {
  441. int width;
  442. unsigned int t1 = 0, t2 = 0;
  443. if (WARN_ON(!cfg80211_chandef_valid(chandef)))
  444. return 0;
  445. width = cfg80211_chandef_get_width(chandef);
  446. if (width < 0)
  447. return 0;
  448. t1 = cfg80211_get_chans_dfs_cac_time(wiphy,
  449. chandef->center_freq1,
  450. width);
  451. if (!chandef->center_freq2)
  452. return t1;
  453. t2 = cfg80211_get_chans_dfs_cac_time(wiphy,
  454. chandef->center_freq2,
  455. width);
  456. return max(t1, t2);
  457. }
  458. static bool cfg80211_secondary_chans_ok(struct wiphy *wiphy,
  459. u32 center_freq, u32 bandwidth,
  460. u32 prohibited_flags)
  461. {
  462. struct ieee80211_channel *c;
  463. u32 freq, start_freq, end_freq;
  464. start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
  465. end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
  466. for (freq = start_freq; freq <= end_freq; freq += 20) {
  467. c = ieee80211_get_channel(wiphy, freq);
  468. if (!c || c->flags & prohibited_flags)
  469. return false;
  470. }
  471. return true;
  472. }
  473. bool cfg80211_chandef_usable(struct wiphy *wiphy,
  474. const struct cfg80211_chan_def *chandef,
  475. u32 prohibited_flags)
  476. {
  477. struct ieee80211_sta_ht_cap *ht_cap;
  478. struct ieee80211_sta_vht_cap *vht_cap;
  479. u32 width, control_freq;
  480. if (WARN_ON(!cfg80211_chandef_valid(chandef)))
  481. return false;
  482. ht_cap = &wiphy->bands[chandef->chan->band]->ht_cap;
  483. vht_cap = &wiphy->bands[chandef->chan->band]->vht_cap;
  484. control_freq = chandef->chan->center_freq;
  485. switch (chandef->width) {
  486. case NL80211_CHAN_WIDTH_5:
  487. width = 5;
  488. break;
  489. case NL80211_CHAN_WIDTH_10:
  490. width = 10;
  491. break;
  492. case NL80211_CHAN_WIDTH_20:
  493. if (!ht_cap->ht_supported)
  494. return false;
  495. case NL80211_CHAN_WIDTH_20_NOHT:
  496. width = 20;
  497. break;
  498. case NL80211_CHAN_WIDTH_40:
  499. width = 40;
  500. if (!ht_cap->ht_supported)
  501. return false;
  502. if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
  503. ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT)
  504. return false;
  505. if (chandef->center_freq1 < control_freq &&
  506. chandef->chan->flags & IEEE80211_CHAN_NO_HT40MINUS)
  507. return false;
  508. if (chandef->center_freq1 > control_freq &&
  509. chandef->chan->flags & IEEE80211_CHAN_NO_HT40PLUS)
  510. return false;
  511. break;
  512. case NL80211_CHAN_WIDTH_80P80:
  513. if (!(vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ))
  514. return false;
  515. case NL80211_CHAN_WIDTH_80:
  516. if (!vht_cap->vht_supported)
  517. return false;
  518. prohibited_flags |= IEEE80211_CHAN_NO_80MHZ;
  519. width = 80;
  520. break;
  521. case NL80211_CHAN_WIDTH_160:
  522. if (!vht_cap->vht_supported)
  523. return false;
  524. if (!(vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ))
  525. return false;
  526. prohibited_flags |= IEEE80211_CHAN_NO_160MHZ;
  527. width = 160;
  528. break;
  529. default:
  530. WARN_ON_ONCE(1);
  531. return false;
  532. }
  533. /*
  534. * TODO: What if there are only certain 80/160/80+80 MHz channels
  535. * allowed by the driver, or only certain combinations?
  536. * For 40 MHz the driver can set the NO_HT40 flags, but for
  537. * 80/160 MHz and in particular 80+80 MHz this isn't really
  538. * feasible and we only have NO_80MHZ/NO_160MHZ so far but
  539. * no way to cover 80+80 MHz or more complex restrictions.
  540. * Note that such restrictions also need to be advertised to
  541. * userspace, for example for P2P channel selection.
  542. */
  543. if (width > 20)
  544. prohibited_flags |= IEEE80211_CHAN_NO_OFDM;
  545. /* 5 and 10 MHz are only defined for the OFDM PHY */
  546. if (width < 20)
  547. prohibited_flags |= IEEE80211_CHAN_NO_OFDM;
  548. if (!cfg80211_secondary_chans_ok(wiphy, chandef->center_freq1,
  549. width, prohibited_flags))
  550. return false;
  551. if (!chandef->center_freq2)
  552. return true;
  553. return cfg80211_secondary_chans_ok(wiphy, chandef->center_freq2,
  554. width, prohibited_flags);
  555. }
  556. EXPORT_SYMBOL(cfg80211_chandef_usable);
  557. bool cfg80211_reg_can_beacon(struct wiphy *wiphy,
  558. struct cfg80211_chan_def *chandef)
  559. {
  560. bool res;
  561. u32 prohibited_flags = IEEE80211_CHAN_DISABLED |
  562. IEEE80211_CHAN_NO_IR |
  563. IEEE80211_CHAN_RADAR;
  564. trace_cfg80211_reg_can_beacon(wiphy, chandef);
  565. if (cfg80211_chandef_dfs_required(wiphy, chandef) > 0 &&
  566. cfg80211_chandef_dfs_available(wiphy, chandef)) {
  567. /* We can skip IEEE80211_CHAN_NO_IR if chandef dfs available */
  568. prohibited_flags = IEEE80211_CHAN_DISABLED;
  569. }
  570. res = cfg80211_chandef_usable(wiphy, chandef, prohibited_flags);
  571. trace_cfg80211_return_bool(res);
  572. return res;
  573. }
  574. EXPORT_SYMBOL(cfg80211_reg_can_beacon);
  575. int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev,
  576. struct cfg80211_chan_def *chandef)
  577. {
  578. if (!rdev->ops->set_monitor_channel)
  579. return -EOPNOTSUPP;
  580. if (!cfg80211_has_monitors_only(rdev))
  581. return -EBUSY;
  582. return rdev_set_monitor_channel(rdev, chandef);
  583. }
  584. void
  585. cfg80211_get_chan_state(struct wireless_dev *wdev,
  586. struct ieee80211_channel **chan,
  587. enum cfg80211_chan_mode *chanmode,
  588. u8 *radar_detect)
  589. {
  590. *chan = NULL;
  591. *chanmode = CHAN_MODE_UNDEFINED;
  592. ASSERT_WDEV_LOCK(wdev);
  593. if (wdev->netdev && !netif_running(wdev->netdev))
  594. return;
  595. switch (wdev->iftype) {
  596. case NL80211_IFTYPE_ADHOC:
  597. if (wdev->current_bss) {
  598. *chan = wdev->current_bss->pub.channel;
  599. *chanmode = (wdev->ibss_fixed &&
  600. !wdev->ibss_dfs_possible)
  601. ? CHAN_MODE_SHARED
  602. : CHAN_MODE_EXCLUSIVE;
  603. /* consider worst-case - IBSS can try to return to the
  604. * original user-specified channel as creator */
  605. if (wdev->ibss_dfs_possible)
  606. *radar_detect |= BIT(wdev->chandef.width);
  607. return;
  608. }
  609. break;
  610. case NL80211_IFTYPE_STATION:
  611. case NL80211_IFTYPE_P2P_CLIENT:
  612. if (wdev->current_bss) {
  613. *chan = wdev->current_bss->pub.channel;
  614. *chanmode = CHAN_MODE_SHARED;
  615. return;
  616. }
  617. break;
  618. case NL80211_IFTYPE_AP:
  619. case NL80211_IFTYPE_P2P_GO:
  620. if (wdev->cac_started) {
  621. *chan = wdev->chandef.chan;
  622. *chanmode = CHAN_MODE_SHARED;
  623. *radar_detect |= BIT(wdev->chandef.width);
  624. } else if (wdev->beacon_interval) {
  625. *chan = wdev->chandef.chan;
  626. *chanmode = CHAN_MODE_SHARED;
  627. if (cfg80211_chandef_dfs_required(wdev->wiphy,
  628. &wdev->chandef))
  629. *radar_detect |= BIT(wdev->chandef.width);
  630. }
  631. return;
  632. case NL80211_IFTYPE_MESH_POINT:
  633. if (wdev->mesh_id_len) {
  634. *chan = wdev->chandef.chan;
  635. *chanmode = CHAN_MODE_SHARED;
  636. if (cfg80211_chandef_dfs_required(wdev->wiphy,
  637. &wdev->chandef))
  638. *radar_detect |= BIT(wdev->chandef.width);
  639. }
  640. return;
  641. case NL80211_IFTYPE_MONITOR:
  642. case NL80211_IFTYPE_AP_VLAN:
  643. case NL80211_IFTYPE_WDS:
  644. case NL80211_IFTYPE_P2P_DEVICE:
  645. /* these interface types don't really have a channel */
  646. return;
  647. case NL80211_IFTYPE_UNSPECIFIED:
  648. case NUM_NL80211_IFTYPES:
  649. WARN_ON(1);
  650. }
  651. }