chan.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*
  2. * This file contains helper code to handle channel
  3. * settings and keeping track of what is possible at
  4. * any point in time.
  5. *
  6. * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
  7. */
  8. #include <linux/export.h>
  9. #include <net/cfg80211.h>
  10. #include "core.h"
  11. #include "rdev-ops.h"
  12. void cfg80211_chandef_create(struct cfg80211_chan_def *chandef,
  13. struct ieee80211_channel *chan,
  14. enum nl80211_channel_type chan_type)
  15. {
  16. if (WARN_ON(!chan))
  17. return;
  18. chandef->chan = chan;
  19. chandef->center_freq2 = 0;
  20. switch (chan_type) {
  21. case NL80211_CHAN_NO_HT:
  22. chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
  23. chandef->center_freq1 = chan->center_freq;
  24. break;
  25. case NL80211_CHAN_HT20:
  26. chandef->width = NL80211_CHAN_WIDTH_20;
  27. chandef->center_freq1 = chan->center_freq;
  28. break;
  29. case NL80211_CHAN_HT40PLUS:
  30. chandef->width = NL80211_CHAN_WIDTH_40;
  31. chandef->center_freq1 = chan->center_freq + 10;
  32. break;
  33. case NL80211_CHAN_HT40MINUS:
  34. chandef->width = NL80211_CHAN_WIDTH_40;
  35. chandef->center_freq1 = chan->center_freq - 10;
  36. break;
  37. default:
  38. WARN_ON(1);
  39. }
  40. }
  41. EXPORT_SYMBOL(cfg80211_chandef_create);
  42. bool cfg80211_chandef_valid(const struct cfg80211_chan_def *chandef)
  43. {
  44. u32 control_freq;
  45. if (!chandef->chan)
  46. return false;
  47. control_freq = chandef->chan->center_freq;
  48. switch (chandef->width) {
  49. case NL80211_CHAN_WIDTH_5:
  50. case NL80211_CHAN_WIDTH_10:
  51. case NL80211_CHAN_WIDTH_20:
  52. case NL80211_CHAN_WIDTH_20_NOHT:
  53. if (chandef->center_freq1 != control_freq)
  54. return false;
  55. if (chandef->center_freq2)
  56. return false;
  57. break;
  58. case NL80211_CHAN_WIDTH_40:
  59. if (chandef->center_freq1 != control_freq + 10 &&
  60. chandef->center_freq1 != control_freq - 10)
  61. return false;
  62. if (chandef->center_freq2)
  63. return false;
  64. break;
  65. case NL80211_CHAN_WIDTH_80P80:
  66. if (chandef->center_freq1 != control_freq + 30 &&
  67. chandef->center_freq1 != control_freq + 10 &&
  68. chandef->center_freq1 != control_freq - 10 &&
  69. chandef->center_freq1 != control_freq - 30)
  70. return false;
  71. if (!chandef->center_freq2)
  72. return false;
  73. /* adjacent is not allowed -- that's a 160 MHz channel */
  74. if (chandef->center_freq1 - chandef->center_freq2 == 80 ||
  75. chandef->center_freq2 - chandef->center_freq1 == 80)
  76. return false;
  77. break;
  78. case NL80211_CHAN_WIDTH_80:
  79. if (chandef->center_freq1 != control_freq + 30 &&
  80. chandef->center_freq1 != control_freq + 10 &&
  81. chandef->center_freq1 != control_freq - 10 &&
  82. chandef->center_freq1 != control_freq - 30)
  83. return false;
  84. if (chandef->center_freq2)
  85. return false;
  86. break;
  87. case NL80211_CHAN_WIDTH_160:
  88. if (chandef->center_freq1 != control_freq + 70 &&
  89. chandef->center_freq1 != control_freq + 50 &&
  90. chandef->center_freq1 != control_freq + 30 &&
  91. chandef->center_freq1 != control_freq + 10 &&
  92. chandef->center_freq1 != control_freq - 10 &&
  93. chandef->center_freq1 != control_freq - 30 &&
  94. chandef->center_freq1 != control_freq - 50 &&
  95. chandef->center_freq1 != control_freq - 70)
  96. return false;
  97. if (chandef->center_freq2)
  98. return false;
  99. break;
  100. default:
  101. return false;
  102. }
  103. return true;
  104. }
  105. EXPORT_SYMBOL(cfg80211_chandef_valid);
  106. static void chandef_primary_freqs(const struct cfg80211_chan_def *c,
  107. int *pri40, int *pri80)
  108. {
  109. int tmp;
  110. switch (c->width) {
  111. case NL80211_CHAN_WIDTH_40:
  112. *pri40 = c->center_freq1;
  113. *pri80 = 0;
  114. break;
  115. case NL80211_CHAN_WIDTH_80:
  116. case NL80211_CHAN_WIDTH_80P80:
  117. *pri80 = c->center_freq1;
  118. /* n_P20 */
  119. tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
  120. /* n_P40 */
  121. tmp /= 2;
  122. /* freq_P40 */
  123. *pri40 = c->center_freq1 - 20 + 40 * tmp;
  124. break;
  125. case NL80211_CHAN_WIDTH_160:
  126. /* n_P20 */
  127. tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
  128. /* n_P40 */
  129. tmp /= 2;
  130. /* freq_P40 */
  131. *pri40 = c->center_freq1 - 60 + 40 * tmp;
  132. /* n_P80 */
  133. tmp /= 2;
  134. *pri80 = c->center_freq1 - 40 + 80 * tmp;
  135. break;
  136. default:
  137. WARN_ON_ONCE(1);
  138. }
  139. }
  140. static int cfg80211_chandef_get_width(const struct cfg80211_chan_def *c)
  141. {
  142. int width;
  143. switch (c->width) {
  144. case NL80211_CHAN_WIDTH_5:
  145. width = 5;
  146. break;
  147. case NL80211_CHAN_WIDTH_10:
  148. width = 10;
  149. break;
  150. case NL80211_CHAN_WIDTH_20:
  151. case NL80211_CHAN_WIDTH_20_NOHT:
  152. width = 20;
  153. break;
  154. case NL80211_CHAN_WIDTH_40:
  155. width = 40;
  156. break;
  157. case NL80211_CHAN_WIDTH_80P80:
  158. case NL80211_CHAN_WIDTH_80:
  159. width = 80;
  160. break;
  161. case NL80211_CHAN_WIDTH_160:
  162. width = 160;
  163. break;
  164. default:
  165. WARN_ON_ONCE(1);
  166. return -1;
  167. }
  168. return width;
  169. }
  170. const struct cfg80211_chan_def *
  171. cfg80211_chandef_compatible(const struct cfg80211_chan_def *c1,
  172. const struct cfg80211_chan_def *c2)
  173. {
  174. u32 c1_pri40, c1_pri80, c2_pri40, c2_pri80;
  175. /* If they are identical, return */
  176. if (cfg80211_chandef_identical(c1, c2))
  177. return c1;
  178. /* otherwise, must have same control channel */
  179. if (c1->chan != c2->chan)
  180. return NULL;
  181. /*
  182. * If they have the same width, but aren't identical,
  183. * then they can't be compatible.
  184. */
  185. if (c1->width == c2->width)
  186. return NULL;
  187. /*
  188. * can't be compatible if one of them is 5 or 10 MHz,
  189. * but they don't have the same width.
  190. */
  191. if (c1->width == NL80211_CHAN_WIDTH_5 ||
  192. c1->width == NL80211_CHAN_WIDTH_10 ||
  193. c2->width == NL80211_CHAN_WIDTH_5 ||
  194. c2->width == NL80211_CHAN_WIDTH_10)
  195. return NULL;
  196. if (c1->width == NL80211_CHAN_WIDTH_20_NOHT ||
  197. c1->width == NL80211_CHAN_WIDTH_20)
  198. return c2;
  199. if (c2->width == NL80211_CHAN_WIDTH_20_NOHT ||
  200. c2->width == NL80211_CHAN_WIDTH_20)
  201. return c1;
  202. chandef_primary_freqs(c1, &c1_pri40, &c1_pri80);
  203. chandef_primary_freqs(c2, &c2_pri40, &c2_pri80);
  204. if (c1_pri40 != c2_pri40)
  205. return NULL;
  206. WARN_ON(!c1_pri80 && !c2_pri80);
  207. if (c1_pri80 && c2_pri80 && c1_pri80 != c2_pri80)
  208. return NULL;
  209. if (c1->width > c2->width)
  210. return c1;
  211. return c2;
  212. }
  213. EXPORT_SYMBOL(cfg80211_chandef_compatible);
  214. static void cfg80211_set_chans_dfs_state(struct wiphy *wiphy, u32 center_freq,
  215. u32 bandwidth,
  216. enum nl80211_dfs_state dfs_state)
  217. {
  218. struct ieee80211_channel *c;
  219. u32 freq;
  220. for (freq = center_freq - bandwidth/2 + 10;
  221. freq <= center_freq + bandwidth/2 - 10;
  222. freq += 20) {
  223. c = ieee80211_get_channel(wiphy, freq);
  224. if (!c || !(c->flags & IEEE80211_CHAN_RADAR))
  225. continue;
  226. c->dfs_state = dfs_state;
  227. c->dfs_state_entered = jiffies;
  228. }
  229. }
  230. void cfg80211_set_dfs_state(struct wiphy *wiphy,
  231. const struct cfg80211_chan_def *chandef,
  232. enum nl80211_dfs_state dfs_state)
  233. {
  234. int width;
  235. if (WARN_ON(!cfg80211_chandef_valid(chandef)))
  236. return;
  237. width = cfg80211_chandef_get_width(chandef);
  238. if (width < 0)
  239. return;
  240. cfg80211_set_chans_dfs_state(wiphy, chandef->center_freq1,
  241. width, dfs_state);
  242. if (!chandef->center_freq2)
  243. return;
  244. cfg80211_set_chans_dfs_state(wiphy, chandef->center_freq2,
  245. width, dfs_state);
  246. }
  247. static int cfg80211_get_chans_dfs_required(struct wiphy *wiphy,
  248. u32 center_freq,
  249. u32 bandwidth)
  250. {
  251. struct ieee80211_channel *c;
  252. u32 freq, start_freq, end_freq;
  253. if (bandwidth <= 20) {
  254. start_freq = center_freq;
  255. end_freq = center_freq;
  256. } else {
  257. start_freq = center_freq - bandwidth/2 + 10;
  258. end_freq = center_freq + bandwidth/2 - 10;
  259. }
  260. for (freq = start_freq; freq <= end_freq; freq += 20) {
  261. c = ieee80211_get_channel(wiphy, freq);
  262. if (!c)
  263. return -EINVAL;
  264. if (c->flags & IEEE80211_CHAN_RADAR)
  265. return 1;
  266. }
  267. return 0;
  268. }
  269. int cfg80211_chandef_dfs_required(struct wiphy *wiphy,
  270. const struct cfg80211_chan_def *chandef)
  271. {
  272. int width;
  273. int r;
  274. if (WARN_ON(!cfg80211_chandef_valid(chandef)))
  275. return -EINVAL;
  276. width = cfg80211_chandef_get_width(chandef);
  277. if (width < 0)
  278. return -EINVAL;
  279. r = cfg80211_get_chans_dfs_required(wiphy, chandef->center_freq1,
  280. width);
  281. if (r)
  282. return r;
  283. if (!chandef->center_freq2)
  284. return 0;
  285. return cfg80211_get_chans_dfs_required(wiphy, chandef->center_freq2,
  286. width);
  287. }
  288. static bool cfg80211_secondary_chans_ok(struct wiphy *wiphy,
  289. u32 center_freq, u32 bandwidth,
  290. u32 prohibited_flags)
  291. {
  292. struct ieee80211_channel *c;
  293. u32 freq, start_freq, end_freq;
  294. if (bandwidth <= 20) {
  295. start_freq = center_freq;
  296. end_freq = center_freq;
  297. } else {
  298. start_freq = center_freq - bandwidth/2 + 10;
  299. end_freq = center_freq + bandwidth/2 - 10;
  300. }
  301. for (freq = start_freq; freq <= end_freq; freq += 20) {
  302. c = ieee80211_get_channel(wiphy, freq);
  303. if (!c)
  304. return false;
  305. /* check for radar flags */
  306. if ((prohibited_flags & c->flags & IEEE80211_CHAN_RADAR) &&
  307. (c->dfs_state != NL80211_DFS_AVAILABLE))
  308. return false;
  309. /* check for the other flags */
  310. if (c->flags & prohibited_flags & ~IEEE80211_CHAN_RADAR)
  311. return false;
  312. }
  313. return true;
  314. }
  315. bool cfg80211_chandef_usable(struct wiphy *wiphy,
  316. const struct cfg80211_chan_def *chandef,
  317. u32 prohibited_flags)
  318. {
  319. struct ieee80211_sta_ht_cap *ht_cap;
  320. struct ieee80211_sta_vht_cap *vht_cap;
  321. u32 width, control_freq;
  322. if (WARN_ON(!cfg80211_chandef_valid(chandef)))
  323. return false;
  324. ht_cap = &wiphy->bands[chandef->chan->band]->ht_cap;
  325. vht_cap = &wiphy->bands[chandef->chan->band]->vht_cap;
  326. control_freq = chandef->chan->center_freq;
  327. switch (chandef->width) {
  328. case NL80211_CHAN_WIDTH_5:
  329. width = 5;
  330. break;
  331. case NL80211_CHAN_WIDTH_10:
  332. width = 10;
  333. break;
  334. case NL80211_CHAN_WIDTH_20:
  335. if (!ht_cap->ht_supported)
  336. return false;
  337. case NL80211_CHAN_WIDTH_20_NOHT:
  338. width = 20;
  339. break;
  340. case NL80211_CHAN_WIDTH_40:
  341. width = 40;
  342. if (!ht_cap->ht_supported)
  343. return false;
  344. if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
  345. ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT)
  346. return false;
  347. if (chandef->center_freq1 < control_freq &&
  348. chandef->chan->flags & IEEE80211_CHAN_NO_HT40MINUS)
  349. return false;
  350. if (chandef->center_freq1 > control_freq &&
  351. chandef->chan->flags & IEEE80211_CHAN_NO_HT40PLUS)
  352. return false;
  353. break;
  354. case NL80211_CHAN_WIDTH_80P80:
  355. if (!(vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ))
  356. return false;
  357. case NL80211_CHAN_WIDTH_80:
  358. if (!vht_cap->vht_supported)
  359. return false;
  360. prohibited_flags |= IEEE80211_CHAN_NO_80MHZ;
  361. width = 80;
  362. break;
  363. case NL80211_CHAN_WIDTH_160:
  364. if (!vht_cap->vht_supported)
  365. return false;
  366. if (!(vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ))
  367. return false;
  368. prohibited_flags |= IEEE80211_CHAN_NO_160MHZ;
  369. width = 160;
  370. break;
  371. default:
  372. WARN_ON_ONCE(1);
  373. return false;
  374. }
  375. /*
  376. * TODO: What if there are only certain 80/160/80+80 MHz channels
  377. * allowed by the driver, or only certain combinations?
  378. * For 40 MHz the driver can set the NO_HT40 flags, but for
  379. * 80/160 MHz and in particular 80+80 MHz this isn't really
  380. * feasible and we only have NO_80MHZ/NO_160MHZ so far but
  381. * no way to cover 80+80 MHz or more complex restrictions.
  382. * Note that such restrictions also need to be advertised to
  383. * userspace, for example for P2P channel selection.
  384. */
  385. if (width > 20)
  386. prohibited_flags |= IEEE80211_CHAN_NO_OFDM;
  387. /* 5 and 10 MHz are only defined for the OFDM PHY */
  388. if (width < 20)
  389. prohibited_flags |= IEEE80211_CHAN_NO_OFDM;
  390. if (!cfg80211_secondary_chans_ok(wiphy, chandef->center_freq1,
  391. width, prohibited_flags))
  392. return false;
  393. if (!chandef->center_freq2)
  394. return true;
  395. return cfg80211_secondary_chans_ok(wiphy, chandef->center_freq2,
  396. width, prohibited_flags);
  397. }
  398. EXPORT_SYMBOL(cfg80211_chandef_usable);
  399. bool cfg80211_reg_can_beacon(struct wiphy *wiphy,
  400. struct cfg80211_chan_def *chandef)
  401. {
  402. bool res;
  403. trace_cfg80211_reg_can_beacon(wiphy, chandef);
  404. res = cfg80211_chandef_usable(wiphy, chandef,
  405. IEEE80211_CHAN_DISABLED |
  406. IEEE80211_CHAN_PASSIVE_SCAN |
  407. IEEE80211_CHAN_NO_IBSS |
  408. IEEE80211_CHAN_RADAR);
  409. trace_cfg80211_return_bool(res);
  410. return res;
  411. }
  412. EXPORT_SYMBOL(cfg80211_reg_can_beacon);
  413. int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev,
  414. struct cfg80211_chan_def *chandef)
  415. {
  416. if (!rdev->ops->set_monitor_channel)
  417. return -EOPNOTSUPP;
  418. if (!cfg80211_has_monitors_only(rdev))
  419. return -EBUSY;
  420. return rdev_set_monitor_channel(rdev, chandef);
  421. }
  422. void
  423. cfg80211_get_chan_state(struct wireless_dev *wdev,
  424. struct ieee80211_channel **chan,
  425. enum cfg80211_chan_mode *chanmode)
  426. {
  427. *chan = NULL;
  428. *chanmode = CHAN_MODE_UNDEFINED;
  429. ASSERT_WDEV_LOCK(wdev);
  430. if (wdev->netdev && !netif_running(wdev->netdev))
  431. return;
  432. switch (wdev->iftype) {
  433. case NL80211_IFTYPE_ADHOC:
  434. if (wdev->current_bss) {
  435. *chan = wdev->current_bss->pub.channel;
  436. *chanmode = wdev->ibss_fixed
  437. ? CHAN_MODE_SHARED
  438. : CHAN_MODE_EXCLUSIVE;
  439. return;
  440. }
  441. case NL80211_IFTYPE_STATION:
  442. case NL80211_IFTYPE_P2P_CLIENT:
  443. if (wdev->current_bss) {
  444. *chan = wdev->current_bss->pub.channel;
  445. *chanmode = CHAN_MODE_SHARED;
  446. return;
  447. }
  448. break;
  449. case NL80211_IFTYPE_AP:
  450. case NL80211_IFTYPE_P2P_GO:
  451. if (wdev->cac_started) {
  452. *chan = wdev->channel;
  453. *chanmode = CHAN_MODE_SHARED;
  454. } else if (wdev->beacon_interval) {
  455. *chan = wdev->channel;
  456. *chanmode = CHAN_MODE_SHARED;
  457. }
  458. return;
  459. case NL80211_IFTYPE_MESH_POINT:
  460. if (wdev->mesh_id_len) {
  461. *chan = wdev->channel;
  462. *chanmode = CHAN_MODE_SHARED;
  463. }
  464. return;
  465. case NL80211_IFTYPE_MONITOR:
  466. case NL80211_IFTYPE_AP_VLAN:
  467. case NL80211_IFTYPE_WDS:
  468. /* these interface types don't really have a channel */
  469. return;
  470. case NL80211_IFTYPE_P2P_DEVICE:
  471. if (wdev->wiphy->features &
  472. NL80211_FEATURE_P2P_DEVICE_NEEDS_CHANNEL)
  473. *chanmode = CHAN_MODE_EXCLUSIVE;
  474. return;
  475. case NL80211_IFTYPE_UNSPECIFIED:
  476. case NUM_NL80211_IFTYPES:
  477. WARN_ON(1);
  478. }
  479. return;
  480. }