chan.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. break;
  72. case NL80211_CHAN_WIDTH_80:
  73. if (chandef->center_freq1 != control_freq + 30 &&
  74. chandef->center_freq1 != control_freq + 10 &&
  75. chandef->center_freq1 != control_freq - 10 &&
  76. chandef->center_freq1 != control_freq - 30)
  77. return false;
  78. if (chandef->center_freq2)
  79. return false;
  80. break;
  81. case NL80211_CHAN_WIDTH_160:
  82. if (chandef->center_freq1 != control_freq + 70 &&
  83. chandef->center_freq1 != control_freq + 50 &&
  84. chandef->center_freq1 != control_freq + 30 &&
  85. chandef->center_freq1 != control_freq + 10 &&
  86. chandef->center_freq1 != control_freq - 10 &&
  87. chandef->center_freq1 != control_freq - 30 &&
  88. chandef->center_freq1 != control_freq - 50 &&
  89. chandef->center_freq1 != control_freq - 70)
  90. return false;
  91. if (chandef->center_freq2)
  92. return false;
  93. break;
  94. default:
  95. return false;
  96. }
  97. return true;
  98. }
  99. EXPORT_SYMBOL(cfg80211_chandef_valid);
  100. static void chandef_primary_freqs(const struct cfg80211_chan_def *c,
  101. int *pri40, int *pri80)
  102. {
  103. int tmp;
  104. switch (c->width) {
  105. case NL80211_CHAN_WIDTH_40:
  106. *pri40 = c->center_freq1;
  107. *pri80 = 0;
  108. break;
  109. case NL80211_CHAN_WIDTH_80:
  110. case NL80211_CHAN_WIDTH_80P80:
  111. *pri80 = c->center_freq1;
  112. /* n_P20 */
  113. tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
  114. /* n_P40 */
  115. tmp /= 2;
  116. /* freq_P40 */
  117. *pri40 = c->center_freq1 - 20 + 40 * tmp;
  118. break;
  119. case NL80211_CHAN_WIDTH_160:
  120. /* n_P20 */
  121. tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
  122. /* n_P40 */
  123. tmp /= 2;
  124. /* freq_P40 */
  125. *pri40 = c->center_freq1 - 60 + 40 * tmp;
  126. /* n_P80 */
  127. tmp /= 2;
  128. *pri80 = c->center_freq1 - 40 + 80 * tmp;
  129. break;
  130. default:
  131. WARN_ON_ONCE(1);
  132. }
  133. }
  134. const struct cfg80211_chan_def *
  135. cfg80211_chandef_compatible(const struct cfg80211_chan_def *c1,
  136. const struct cfg80211_chan_def *c2)
  137. {
  138. u32 c1_pri40, c1_pri80, c2_pri40, c2_pri80;
  139. /* If they are identical, return */
  140. if (cfg80211_chandef_identical(c1, c2))
  141. return c1;
  142. /* otherwise, must have same control channel */
  143. if (c1->chan != c2->chan)
  144. return NULL;
  145. /*
  146. * If they have the same width, but aren't identical,
  147. * then they can't be compatible.
  148. */
  149. if (c1->width == c2->width)
  150. return NULL;
  151. if (c1->width == NL80211_CHAN_WIDTH_20_NOHT ||
  152. c1->width == NL80211_CHAN_WIDTH_20)
  153. return c2;
  154. if (c2->width == NL80211_CHAN_WIDTH_20_NOHT ||
  155. c2->width == NL80211_CHAN_WIDTH_20)
  156. return c1;
  157. chandef_primary_freqs(c1, &c1_pri40, &c1_pri80);
  158. chandef_primary_freqs(c2, &c2_pri40, &c2_pri80);
  159. if (c1_pri40 != c2_pri40)
  160. return NULL;
  161. WARN_ON(!c1_pri80 && !c2_pri80);
  162. if (c1_pri80 && c2_pri80 && c1_pri80 != c2_pri80)
  163. return NULL;
  164. if (c1->width > c2->width)
  165. return c1;
  166. return c2;
  167. }
  168. EXPORT_SYMBOL(cfg80211_chandef_compatible);
  169. static bool cfg80211_secondary_chans_ok(struct wiphy *wiphy,
  170. u32 center_freq, u32 bandwidth,
  171. u32 prohibited_flags)
  172. {
  173. struct ieee80211_channel *c;
  174. u32 freq;
  175. for (freq = center_freq - bandwidth/2 + 10;
  176. freq <= center_freq + bandwidth/2 - 10;
  177. freq += 20) {
  178. c = ieee80211_get_channel(wiphy, freq);
  179. if (!c || c->flags & prohibited_flags)
  180. return false;
  181. }
  182. return true;
  183. }
  184. bool cfg80211_chandef_usable(struct wiphy *wiphy,
  185. const struct cfg80211_chan_def *chandef,
  186. u32 prohibited_flags)
  187. {
  188. struct ieee80211_sta_ht_cap *ht_cap;
  189. struct ieee80211_sta_vht_cap *vht_cap;
  190. u32 width, control_freq;
  191. if (WARN_ON(!cfg80211_chandef_valid(chandef)))
  192. return false;
  193. ht_cap = &wiphy->bands[chandef->chan->band]->ht_cap;
  194. vht_cap = &wiphy->bands[chandef->chan->band]->vht_cap;
  195. control_freq = chandef->chan->center_freq;
  196. switch (chandef->width) {
  197. case NL80211_CHAN_WIDTH_20:
  198. if (!ht_cap->ht_supported)
  199. return false;
  200. case NL80211_CHAN_WIDTH_20_NOHT:
  201. width = 20;
  202. break;
  203. case NL80211_CHAN_WIDTH_40:
  204. width = 40;
  205. if (!ht_cap->ht_supported)
  206. return false;
  207. if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
  208. ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT)
  209. return false;
  210. if (chandef->center_freq1 < control_freq &&
  211. chandef->chan->flags & IEEE80211_CHAN_NO_HT40MINUS)
  212. return false;
  213. if (chandef->center_freq1 > control_freq &&
  214. chandef->chan->flags & IEEE80211_CHAN_NO_HT40PLUS)
  215. return false;
  216. break;
  217. case NL80211_CHAN_WIDTH_80P80:
  218. if (!(vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ))
  219. return false;
  220. case NL80211_CHAN_WIDTH_80:
  221. if (!vht_cap->vht_supported)
  222. return false;
  223. width = 80;
  224. break;
  225. case NL80211_CHAN_WIDTH_160:
  226. if (!vht_cap->vht_supported)
  227. return false;
  228. if (!(vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ))
  229. return false;
  230. width = 160;
  231. break;
  232. default:
  233. WARN_ON_ONCE(1);
  234. return false;
  235. }
  236. /* TODO: missing regulatory check on 80/160 bandwidth */
  237. if (width > 20)
  238. prohibited_flags |= IEEE80211_CHAN_NO_OFDM;
  239. if (!cfg80211_secondary_chans_ok(wiphy, chandef->center_freq1,
  240. width, prohibited_flags))
  241. return false;
  242. if (!chandef->center_freq2)
  243. return true;
  244. return cfg80211_secondary_chans_ok(wiphy, chandef->center_freq2,
  245. width, prohibited_flags);
  246. }
  247. EXPORT_SYMBOL(cfg80211_chandef_usable);
  248. bool cfg80211_reg_can_beacon(struct wiphy *wiphy,
  249. struct cfg80211_chan_def *chandef)
  250. {
  251. bool res;
  252. trace_cfg80211_reg_can_beacon(wiphy, chandef);
  253. res = cfg80211_chandef_usable(wiphy, chandef,
  254. IEEE80211_CHAN_DISABLED |
  255. IEEE80211_CHAN_PASSIVE_SCAN |
  256. IEEE80211_CHAN_NO_IBSS |
  257. IEEE80211_CHAN_RADAR);
  258. trace_cfg80211_return_bool(res);
  259. return res;
  260. }
  261. EXPORT_SYMBOL(cfg80211_reg_can_beacon);
  262. int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev,
  263. struct cfg80211_chan_def *chandef)
  264. {
  265. if (!rdev->ops->set_monitor_channel)
  266. return -EOPNOTSUPP;
  267. if (!cfg80211_has_monitors_only(rdev))
  268. return -EBUSY;
  269. return rdev_set_monitor_channel(rdev, chandef);
  270. }
  271. void
  272. cfg80211_get_chan_state(struct wireless_dev *wdev,
  273. struct ieee80211_channel **chan,
  274. enum cfg80211_chan_mode *chanmode)
  275. {
  276. *chan = NULL;
  277. *chanmode = CHAN_MODE_UNDEFINED;
  278. ASSERT_WDEV_LOCK(wdev);
  279. if (wdev->netdev && !netif_running(wdev->netdev))
  280. return;
  281. switch (wdev->iftype) {
  282. case NL80211_IFTYPE_ADHOC:
  283. if (wdev->current_bss) {
  284. *chan = wdev->current_bss->pub.channel;
  285. *chanmode = wdev->ibss_fixed
  286. ? CHAN_MODE_SHARED
  287. : CHAN_MODE_EXCLUSIVE;
  288. return;
  289. }
  290. case NL80211_IFTYPE_STATION:
  291. case NL80211_IFTYPE_P2P_CLIENT:
  292. if (wdev->current_bss) {
  293. *chan = wdev->current_bss->pub.channel;
  294. *chanmode = CHAN_MODE_SHARED;
  295. return;
  296. }
  297. break;
  298. case NL80211_IFTYPE_AP:
  299. case NL80211_IFTYPE_P2P_GO:
  300. if (wdev->beacon_interval) {
  301. *chan = wdev->channel;
  302. *chanmode = CHAN_MODE_SHARED;
  303. }
  304. return;
  305. case NL80211_IFTYPE_MESH_POINT:
  306. if (wdev->mesh_id_len) {
  307. *chan = wdev->channel;
  308. *chanmode = CHAN_MODE_SHARED;
  309. }
  310. return;
  311. case NL80211_IFTYPE_MONITOR:
  312. case NL80211_IFTYPE_AP_VLAN:
  313. case NL80211_IFTYPE_WDS:
  314. /* these interface types don't really have a channel */
  315. return;
  316. case NL80211_IFTYPE_P2P_DEVICE:
  317. if (wdev->wiphy->features &
  318. NL80211_FEATURE_P2P_DEVICE_NEEDS_CHANNEL)
  319. *chanmode = CHAN_MODE_EXCLUSIVE;
  320. return;
  321. case NL80211_IFTYPE_UNSPECIFIED:
  322. case NUM_NL80211_IFTYPES:
  323. WARN_ON(1);
  324. }
  325. return;
  326. }