chan.c 26 KB

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