chan.c 24 KB

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