chan.c 12 KB

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