chan.c 26 KB

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