chan.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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. enum nl80211_iftype iftype)
  287. {
  288. int width;
  289. int ret;
  290. if (WARN_ON(!cfg80211_chandef_valid(chandef)))
  291. return -EINVAL;
  292. switch (iftype) {
  293. case NL80211_IFTYPE_ADHOC:
  294. case NL80211_IFTYPE_AP:
  295. case NL80211_IFTYPE_P2P_GO:
  296. case NL80211_IFTYPE_MESH_POINT:
  297. width = cfg80211_chandef_get_width(chandef);
  298. if (width < 0)
  299. return -EINVAL;
  300. ret = cfg80211_get_chans_dfs_required(wiphy,
  301. chandef->center_freq1,
  302. width);
  303. if (ret < 0)
  304. return ret;
  305. else if (ret > 0)
  306. return BIT(chandef->width);
  307. if (!chandef->center_freq2)
  308. return 0;
  309. ret = cfg80211_get_chans_dfs_required(wiphy,
  310. chandef->center_freq2,
  311. width);
  312. if (ret < 0)
  313. return ret;
  314. else if (ret > 0)
  315. return BIT(chandef->width);
  316. break;
  317. case NL80211_IFTYPE_STATION:
  318. case NL80211_IFTYPE_P2P_CLIENT:
  319. case NL80211_IFTYPE_MONITOR:
  320. case NL80211_IFTYPE_AP_VLAN:
  321. case NL80211_IFTYPE_WDS:
  322. case NL80211_IFTYPE_P2P_DEVICE:
  323. break;
  324. case NL80211_IFTYPE_UNSPECIFIED:
  325. case NUM_NL80211_IFTYPES:
  326. WARN_ON(1);
  327. }
  328. return 0;
  329. }
  330. EXPORT_SYMBOL(cfg80211_chandef_dfs_required);
  331. static int cfg80211_get_chans_dfs_usable(struct wiphy *wiphy,
  332. u32 center_freq,
  333. u32 bandwidth)
  334. {
  335. struct ieee80211_channel *c;
  336. u32 freq, start_freq, end_freq;
  337. int count = 0;
  338. start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
  339. end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
  340. /*
  341. * Check entire range of channels for the bandwidth.
  342. * Check all channels are DFS channels (DFS_USABLE or
  343. * DFS_AVAILABLE). Return number of usable channels
  344. * (require CAC). Allow DFS and non-DFS channel mix.
  345. */
  346. for (freq = start_freq; freq <= end_freq; freq += 20) {
  347. c = ieee80211_get_channel(wiphy, freq);
  348. if (!c)
  349. return -EINVAL;
  350. if (c->flags & IEEE80211_CHAN_DISABLED)
  351. return -EINVAL;
  352. if (c->flags & IEEE80211_CHAN_RADAR) {
  353. if (c->dfs_state == NL80211_DFS_UNAVAILABLE)
  354. return -EINVAL;
  355. if (c->dfs_state == NL80211_DFS_USABLE)
  356. count++;
  357. }
  358. }
  359. return count;
  360. }
  361. bool cfg80211_chandef_dfs_usable(struct wiphy *wiphy,
  362. const struct cfg80211_chan_def *chandef)
  363. {
  364. int width;
  365. int r1, r2 = 0;
  366. if (WARN_ON(!cfg80211_chandef_valid(chandef)))
  367. return false;
  368. width = cfg80211_chandef_get_width(chandef);
  369. if (width < 0)
  370. return false;
  371. r1 = cfg80211_get_chans_dfs_usable(wiphy, chandef->center_freq1,
  372. width);
  373. if (r1 < 0)
  374. return false;
  375. switch (chandef->width) {
  376. case NL80211_CHAN_WIDTH_80P80:
  377. WARN_ON(!chandef->center_freq2);
  378. r2 = cfg80211_get_chans_dfs_usable(wiphy,
  379. chandef->center_freq2,
  380. width);
  381. if (r2 < 0)
  382. return false;
  383. break;
  384. default:
  385. WARN_ON(chandef->center_freq2);
  386. break;
  387. }
  388. return (r1 + r2 > 0);
  389. }
  390. static bool cfg80211_get_chans_dfs_available(struct wiphy *wiphy,
  391. u32 center_freq,
  392. u32 bandwidth)
  393. {
  394. struct ieee80211_channel *c;
  395. u32 freq, start_freq, end_freq;
  396. start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
  397. end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
  398. /*
  399. * Check entire range of channels for the bandwidth.
  400. * If any channel in between is disabled or has not
  401. * had gone through CAC return false
  402. */
  403. for (freq = start_freq; freq <= end_freq; freq += 20) {
  404. c = ieee80211_get_channel(wiphy, freq);
  405. if (!c)
  406. return false;
  407. if (c->flags & IEEE80211_CHAN_DISABLED)
  408. return false;
  409. if ((c->flags & IEEE80211_CHAN_RADAR) &&
  410. (c->dfs_state != NL80211_DFS_AVAILABLE))
  411. return false;
  412. }
  413. return true;
  414. }
  415. static bool cfg80211_chandef_dfs_available(struct wiphy *wiphy,
  416. const struct cfg80211_chan_def *chandef)
  417. {
  418. int width;
  419. int r;
  420. if (WARN_ON(!cfg80211_chandef_valid(chandef)))
  421. return false;
  422. width = cfg80211_chandef_get_width(chandef);
  423. if (width < 0)
  424. return false;
  425. r = cfg80211_get_chans_dfs_available(wiphy, chandef->center_freq1,
  426. width);
  427. /* If any of channels unavailable for cf1 just return */
  428. if (!r)
  429. return r;
  430. switch (chandef->width) {
  431. case NL80211_CHAN_WIDTH_80P80:
  432. WARN_ON(!chandef->center_freq2);
  433. r = cfg80211_get_chans_dfs_available(wiphy,
  434. chandef->center_freq2,
  435. width);
  436. default:
  437. WARN_ON(chandef->center_freq2);
  438. break;
  439. }
  440. return r;
  441. }
  442. static unsigned int cfg80211_get_chans_dfs_cac_time(struct wiphy *wiphy,
  443. u32 center_freq,
  444. u32 bandwidth)
  445. {
  446. struct ieee80211_channel *c;
  447. u32 start_freq, end_freq, freq;
  448. unsigned int dfs_cac_ms = 0;
  449. start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
  450. end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
  451. for (freq = start_freq; freq <= end_freq; freq += 20) {
  452. c = ieee80211_get_channel(wiphy, freq);
  453. if (!c)
  454. return 0;
  455. if (c->flags & IEEE80211_CHAN_DISABLED)
  456. return 0;
  457. if (!(c->flags & IEEE80211_CHAN_RADAR))
  458. continue;
  459. if (c->dfs_cac_ms > dfs_cac_ms)
  460. dfs_cac_ms = c->dfs_cac_ms;
  461. }
  462. return dfs_cac_ms;
  463. }
  464. unsigned int
  465. cfg80211_chandef_dfs_cac_time(struct wiphy *wiphy,
  466. const struct cfg80211_chan_def *chandef)
  467. {
  468. int width;
  469. unsigned int t1 = 0, t2 = 0;
  470. if (WARN_ON(!cfg80211_chandef_valid(chandef)))
  471. return 0;
  472. width = cfg80211_chandef_get_width(chandef);
  473. if (width < 0)
  474. return 0;
  475. t1 = cfg80211_get_chans_dfs_cac_time(wiphy,
  476. chandef->center_freq1,
  477. width);
  478. if (!chandef->center_freq2)
  479. return t1;
  480. t2 = cfg80211_get_chans_dfs_cac_time(wiphy,
  481. chandef->center_freq2,
  482. width);
  483. return max(t1, t2);
  484. }
  485. static bool cfg80211_secondary_chans_ok(struct wiphy *wiphy,
  486. u32 center_freq, u32 bandwidth,
  487. u32 prohibited_flags)
  488. {
  489. struct ieee80211_channel *c;
  490. u32 freq, start_freq, end_freq;
  491. start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
  492. end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
  493. for (freq = start_freq; freq <= end_freq; freq += 20) {
  494. c = ieee80211_get_channel(wiphy, freq);
  495. if (!c || c->flags & prohibited_flags)
  496. return false;
  497. }
  498. return true;
  499. }
  500. bool cfg80211_chandef_usable(struct wiphy *wiphy,
  501. const struct cfg80211_chan_def *chandef,
  502. u32 prohibited_flags)
  503. {
  504. struct ieee80211_sta_ht_cap *ht_cap;
  505. struct ieee80211_sta_vht_cap *vht_cap;
  506. u32 width, control_freq;
  507. if (WARN_ON(!cfg80211_chandef_valid(chandef)))
  508. return false;
  509. ht_cap = &wiphy->bands[chandef->chan->band]->ht_cap;
  510. vht_cap = &wiphy->bands[chandef->chan->band]->vht_cap;
  511. control_freq = chandef->chan->center_freq;
  512. switch (chandef->width) {
  513. case NL80211_CHAN_WIDTH_5:
  514. width = 5;
  515. break;
  516. case NL80211_CHAN_WIDTH_10:
  517. prohibited_flags |= IEEE80211_CHAN_NO_10MHZ;
  518. width = 10;
  519. break;
  520. case NL80211_CHAN_WIDTH_20:
  521. if (!ht_cap->ht_supported)
  522. return false;
  523. case NL80211_CHAN_WIDTH_20_NOHT:
  524. prohibited_flags |= IEEE80211_CHAN_NO_20MHZ;
  525. width = 20;
  526. break;
  527. case NL80211_CHAN_WIDTH_40:
  528. width = 40;
  529. if (!ht_cap->ht_supported)
  530. return false;
  531. if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
  532. ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT)
  533. return false;
  534. if (chandef->center_freq1 < control_freq &&
  535. chandef->chan->flags & IEEE80211_CHAN_NO_HT40MINUS)
  536. return false;
  537. if (chandef->center_freq1 > control_freq &&
  538. chandef->chan->flags & IEEE80211_CHAN_NO_HT40PLUS)
  539. return false;
  540. break;
  541. case NL80211_CHAN_WIDTH_80P80:
  542. if (!(vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ))
  543. return false;
  544. case NL80211_CHAN_WIDTH_80:
  545. if (!vht_cap->vht_supported)
  546. return false;
  547. prohibited_flags |= IEEE80211_CHAN_NO_80MHZ;
  548. width = 80;
  549. break;
  550. case NL80211_CHAN_WIDTH_160:
  551. if (!vht_cap->vht_supported)
  552. return false;
  553. if (!(vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ))
  554. return false;
  555. prohibited_flags |= IEEE80211_CHAN_NO_160MHZ;
  556. width = 160;
  557. break;
  558. default:
  559. WARN_ON_ONCE(1);
  560. return false;
  561. }
  562. /*
  563. * TODO: What if there are only certain 80/160/80+80 MHz channels
  564. * allowed by the driver, or only certain combinations?
  565. * For 40 MHz the driver can set the NO_HT40 flags, but for
  566. * 80/160 MHz and in particular 80+80 MHz this isn't really
  567. * feasible and we only have NO_80MHZ/NO_160MHZ so far but
  568. * no way to cover 80+80 MHz or more complex restrictions.
  569. * Note that such restrictions also need to be advertised to
  570. * userspace, for example for P2P channel selection.
  571. */
  572. if (width > 20)
  573. prohibited_flags |= IEEE80211_CHAN_NO_OFDM;
  574. /* 5 and 10 MHz are only defined for the OFDM PHY */
  575. if (width < 20)
  576. prohibited_flags |= IEEE80211_CHAN_NO_OFDM;
  577. if (!cfg80211_secondary_chans_ok(wiphy, chandef->center_freq1,
  578. width, prohibited_flags))
  579. return false;
  580. if (!chandef->center_freq2)
  581. return true;
  582. return cfg80211_secondary_chans_ok(wiphy, chandef->center_freq2,
  583. width, prohibited_flags);
  584. }
  585. EXPORT_SYMBOL(cfg80211_chandef_usable);
  586. /*
  587. * For GO only, check if the channel can be used under permissive conditions
  588. * mandated by the some regulatory bodies, i.e., the channel is marked with
  589. * IEEE80211_CHAN_GO_CONCURRENT and there is an additional station interface
  590. * associated to an AP on the same channel or on the same UNII band
  591. * (assuming that the AP is an authorized master).
  592. * In addition allow the GO to operate on a channel on which indoor operation is
  593. * allowed, iff we are currently operating in an indoor environment.
  594. */
  595. static bool cfg80211_go_permissive_chan(struct cfg80211_registered_device *rdev,
  596. struct ieee80211_channel *chan)
  597. {
  598. struct wireless_dev *wdev_iter;
  599. struct wiphy *wiphy = wiphy_idx_to_wiphy(rdev->wiphy_idx);
  600. ASSERT_RTNL();
  601. if (!config_enabled(CONFIG_CFG80211_REG_RELAX_NO_IR) ||
  602. !(wiphy->regulatory_flags & REGULATORY_ENABLE_RELAX_NO_IR))
  603. return false;
  604. if (regulatory_indoor_allowed() &&
  605. (chan->flags & IEEE80211_CHAN_INDOOR_ONLY))
  606. return true;
  607. if (!(chan->flags & IEEE80211_CHAN_GO_CONCURRENT))
  608. return false;
  609. /*
  610. * Generally, it is possible to rely on another device/driver to allow
  611. * the GO concurrent relaxation, however, since the device can further
  612. * enforce the relaxation (by doing a similar verifications as this),
  613. * and thus fail the GO instantiation, consider only the interfaces of
  614. * the current registered device.
  615. */
  616. list_for_each_entry(wdev_iter, &rdev->wdev_list, list) {
  617. struct ieee80211_channel *other_chan = NULL;
  618. int r1, r2;
  619. if (wdev_iter->iftype != NL80211_IFTYPE_STATION ||
  620. !netif_running(wdev_iter->netdev))
  621. continue;
  622. wdev_lock(wdev_iter);
  623. if (wdev_iter->current_bss)
  624. other_chan = wdev_iter->current_bss->pub.channel;
  625. wdev_unlock(wdev_iter);
  626. if (!other_chan)
  627. continue;
  628. if (chan == other_chan)
  629. return true;
  630. if (chan->band != IEEE80211_BAND_5GHZ)
  631. continue;
  632. r1 = cfg80211_get_unii(chan->center_freq);
  633. r2 = cfg80211_get_unii(other_chan->center_freq);
  634. if (r1 != -EINVAL && r1 == r2) {
  635. /*
  636. * At some locations channels 149-165 are considered a
  637. * bundle, but at other locations, e.g., Indonesia,
  638. * channels 149-161 are considered a bundle while
  639. * channel 165 is left out and considered to be in a
  640. * different bundle. Thus, in case that there is a
  641. * station interface connected to an AP on channel 165,
  642. * it is assumed that channels 149-161 are allowed for
  643. * GO operations. However, having a station interface
  644. * connected to an AP on channels 149-161, does not
  645. * allow GO operation on channel 165.
  646. */
  647. if (chan->center_freq == 5825 &&
  648. other_chan->center_freq != 5825)
  649. continue;
  650. return true;
  651. }
  652. }
  653. return false;
  654. }
  655. bool cfg80211_reg_can_beacon(struct wiphy *wiphy,
  656. struct cfg80211_chan_def *chandef,
  657. enum nl80211_iftype iftype)
  658. {
  659. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  660. bool res;
  661. u32 prohibited_flags = IEEE80211_CHAN_DISABLED |
  662. IEEE80211_CHAN_RADAR;
  663. trace_cfg80211_reg_can_beacon(wiphy, chandef, iftype);
  664. /*
  665. * Under certain conditions suggested by the some regulatory bodies
  666. * a GO can operate on channels marked with IEEE80211_NO_IR
  667. * so set this flag only if such relaxations are not enabled and
  668. * the conditions are not met.
  669. */
  670. if (iftype != NL80211_IFTYPE_P2P_GO ||
  671. !cfg80211_go_permissive_chan(rdev, chandef->chan))
  672. prohibited_flags |= IEEE80211_CHAN_NO_IR;
  673. if (cfg80211_chandef_dfs_required(wiphy, chandef, iftype) > 0 &&
  674. cfg80211_chandef_dfs_available(wiphy, chandef)) {
  675. /* We can skip IEEE80211_CHAN_NO_IR if chandef dfs available */
  676. prohibited_flags = IEEE80211_CHAN_DISABLED;
  677. }
  678. res = cfg80211_chandef_usable(wiphy, chandef, prohibited_flags);
  679. trace_cfg80211_return_bool(res);
  680. return res;
  681. }
  682. EXPORT_SYMBOL(cfg80211_reg_can_beacon);
  683. int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev,
  684. struct cfg80211_chan_def *chandef)
  685. {
  686. if (!rdev->ops->set_monitor_channel)
  687. return -EOPNOTSUPP;
  688. if (!cfg80211_has_monitors_only(rdev))
  689. return -EBUSY;
  690. return rdev_set_monitor_channel(rdev, chandef);
  691. }
  692. void
  693. cfg80211_get_chan_state(struct wireless_dev *wdev,
  694. struct ieee80211_channel **chan,
  695. enum cfg80211_chan_mode *chanmode,
  696. u8 *radar_detect)
  697. {
  698. int ret;
  699. *chan = NULL;
  700. *chanmode = CHAN_MODE_UNDEFINED;
  701. ASSERT_WDEV_LOCK(wdev);
  702. if (wdev->netdev && !netif_running(wdev->netdev))
  703. return;
  704. switch (wdev->iftype) {
  705. case NL80211_IFTYPE_ADHOC:
  706. if (wdev->current_bss) {
  707. *chan = wdev->current_bss->pub.channel;
  708. *chanmode = (wdev->ibss_fixed &&
  709. !wdev->ibss_dfs_possible)
  710. ? CHAN_MODE_SHARED
  711. : CHAN_MODE_EXCLUSIVE;
  712. /* consider worst-case - IBSS can try to return to the
  713. * original user-specified channel as creator */
  714. if (wdev->ibss_dfs_possible)
  715. *radar_detect |= BIT(wdev->chandef.width);
  716. return;
  717. }
  718. break;
  719. case NL80211_IFTYPE_STATION:
  720. case NL80211_IFTYPE_P2P_CLIENT:
  721. if (wdev->current_bss) {
  722. *chan = wdev->current_bss->pub.channel;
  723. *chanmode = CHAN_MODE_SHARED;
  724. return;
  725. }
  726. break;
  727. case NL80211_IFTYPE_AP:
  728. case NL80211_IFTYPE_P2P_GO:
  729. if (wdev->cac_started) {
  730. *chan = wdev->chandef.chan;
  731. *chanmode = CHAN_MODE_SHARED;
  732. *radar_detect |= BIT(wdev->chandef.width);
  733. } else if (wdev->beacon_interval) {
  734. *chan = wdev->chandef.chan;
  735. *chanmode = CHAN_MODE_SHARED;
  736. ret = cfg80211_chandef_dfs_required(wdev->wiphy,
  737. &wdev->chandef,
  738. wdev->iftype);
  739. WARN_ON(ret < 0);
  740. if (ret > 0)
  741. *radar_detect |= BIT(wdev->chandef.width);
  742. }
  743. return;
  744. case NL80211_IFTYPE_MESH_POINT:
  745. if (wdev->mesh_id_len) {
  746. *chan = wdev->chandef.chan;
  747. *chanmode = CHAN_MODE_SHARED;
  748. ret = cfg80211_chandef_dfs_required(wdev->wiphy,
  749. &wdev->chandef,
  750. wdev->iftype);
  751. WARN_ON(ret < 0);
  752. if (ret > 0)
  753. *radar_detect |= BIT(wdev->chandef.width);
  754. }
  755. return;
  756. case NL80211_IFTYPE_MONITOR:
  757. case NL80211_IFTYPE_AP_VLAN:
  758. case NL80211_IFTYPE_WDS:
  759. case NL80211_IFTYPE_P2P_DEVICE:
  760. /* these interface types don't really have a channel */
  761. return;
  762. case NL80211_IFTYPE_UNSPECIFIED:
  763. case NUM_NL80211_IFTYPES:
  764. WARN_ON(1);
  765. }
  766. }