chan.c 9.2 KB

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