chan.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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_chan_def_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. static void chandef_primary_freqs(const struct cfg80211_chan_def *c,
  100. int *pri40, int *pri80)
  101. {
  102. int tmp;
  103. switch (c->width) {
  104. case NL80211_CHAN_WIDTH_40:
  105. *pri40 = c->center_freq1;
  106. *pri80 = 0;
  107. break;
  108. case NL80211_CHAN_WIDTH_80:
  109. case NL80211_CHAN_WIDTH_80P80:
  110. *pri80 = c->center_freq1;
  111. /* n_P20 */
  112. tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
  113. /* n_P40 */
  114. tmp /= 2;
  115. /* freq_P40 */
  116. *pri40 = c->center_freq1 - 20 + 40 * tmp;
  117. break;
  118. case NL80211_CHAN_WIDTH_160:
  119. /* n_P20 */
  120. tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
  121. /* n_P40 */
  122. tmp /= 2;
  123. /* freq_P40 */
  124. *pri40 = c->center_freq1 - 60 + 40 * tmp;
  125. /* n_P80 */
  126. tmp /= 2;
  127. *pri80 = c->center_freq1 - 40 + 80 * tmp;
  128. break;
  129. default:
  130. WARN_ON_ONCE(1);
  131. }
  132. }
  133. const struct cfg80211_chan_def *
  134. cfg80211_chandef_compatible(const struct cfg80211_chan_def *c1,
  135. const struct cfg80211_chan_def *c2)
  136. {
  137. u32 c1_pri40, c1_pri80, c2_pri40, c2_pri80;
  138. /* If they are identical, return */
  139. if (cfg80211_chandef_identical(c1, c2))
  140. return c1;
  141. /* otherwise, must have same control channel */
  142. if (c1->chan != c2->chan)
  143. return NULL;
  144. /*
  145. * If they have the same width, but aren't identical,
  146. * then they can't be compatible.
  147. */
  148. if (c1->width == c2->width)
  149. return NULL;
  150. if (c1->width == NL80211_CHAN_WIDTH_20_NOHT ||
  151. c1->width == NL80211_CHAN_WIDTH_20)
  152. return c2;
  153. if (c2->width == NL80211_CHAN_WIDTH_20_NOHT ||
  154. c2->width == NL80211_CHAN_WIDTH_20)
  155. return c1;
  156. chandef_primary_freqs(c1, &c1_pri40, &c1_pri80);
  157. chandef_primary_freqs(c2, &c2_pri40, &c2_pri80);
  158. if (c1_pri40 != c2_pri40)
  159. return NULL;
  160. WARN_ON(!c1_pri80 && !c2_pri80);
  161. if (c1_pri80 && c2_pri80 && c1_pri80 != c2_pri80)
  162. return NULL;
  163. if (c1->width > c2->width)
  164. return c1;
  165. return c2;
  166. }
  167. EXPORT_SYMBOL(cfg80211_chandef_compatible);
  168. bool cfg80211_secondary_chans_ok(struct wiphy *wiphy,
  169. u32 center_freq, u32 bandwidth,
  170. u32 prohibited_flags)
  171. {
  172. struct ieee80211_channel *c;
  173. u32 freq;
  174. for (freq = center_freq - bandwidth/2 + 10;
  175. freq <= center_freq + bandwidth/2 - 10;
  176. freq += 20) {
  177. c = ieee80211_get_channel(wiphy, freq);
  178. if (!c || c->flags & prohibited_flags)
  179. return false;
  180. }
  181. return true;
  182. }
  183. static bool cfg80211_check_beacon_chans(struct wiphy *wiphy,
  184. u32 center_freq, u32 bw)
  185. {
  186. return cfg80211_secondary_chans_ok(wiphy, center_freq, bw,
  187. IEEE80211_CHAN_DISABLED |
  188. IEEE80211_CHAN_PASSIVE_SCAN |
  189. IEEE80211_CHAN_NO_IBSS |
  190. IEEE80211_CHAN_RADAR);
  191. }
  192. bool cfg80211_reg_can_beacon(struct wiphy *wiphy,
  193. struct cfg80211_chan_def *chandef)
  194. {
  195. u32 width;
  196. bool res;
  197. trace_cfg80211_reg_can_beacon(wiphy, chandef);
  198. if (WARN_ON(!cfg80211_chan_def_valid(chandef))) {
  199. trace_cfg80211_return_bool(false);
  200. return false;
  201. }
  202. switch (chandef->width) {
  203. case NL80211_CHAN_WIDTH_20_NOHT:
  204. case NL80211_CHAN_WIDTH_20:
  205. width = 20;
  206. break;
  207. case NL80211_CHAN_WIDTH_40:
  208. width = 40;
  209. break;
  210. case NL80211_CHAN_WIDTH_80:
  211. case NL80211_CHAN_WIDTH_80P80:
  212. width = 80;
  213. break;
  214. case NL80211_CHAN_WIDTH_160:
  215. width = 160;
  216. break;
  217. default:
  218. WARN_ON_ONCE(1);
  219. trace_cfg80211_return_bool(false);
  220. return false;
  221. }
  222. res = cfg80211_check_beacon_chans(wiphy, chandef->center_freq1, width);
  223. if (res && chandef->center_freq2)
  224. res = cfg80211_check_beacon_chans(wiphy, chandef->center_freq2,
  225. width);
  226. trace_cfg80211_return_bool(res);
  227. return res;
  228. }
  229. EXPORT_SYMBOL(cfg80211_reg_can_beacon);
  230. int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev,
  231. struct cfg80211_chan_def *chandef)
  232. {
  233. if (!rdev->ops->set_monitor_channel)
  234. return -EOPNOTSUPP;
  235. if (!cfg80211_has_monitors_only(rdev))
  236. return -EBUSY;
  237. return rdev_set_monitor_channel(rdev, chandef);
  238. }
  239. void
  240. cfg80211_get_chan_state(struct wireless_dev *wdev,
  241. struct ieee80211_channel **chan,
  242. enum cfg80211_chan_mode *chanmode)
  243. {
  244. *chan = NULL;
  245. *chanmode = CHAN_MODE_UNDEFINED;
  246. ASSERT_WDEV_LOCK(wdev);
  247. if (wdev->netdev && !netif_running(wdev->netdev))
  248. return;
  249. switch (wdev->iftype) {
  250. case NL80211_IFTYPE_ADHOC:
  251. if (wdev->current_bss) {
  252. *chan = wdev->current_bss->pub.channel;
  253. *chanmode = wdev->ibss_fixed
  254. ? CHAN_MODE_SHARED
  255. : CHAN_MODE_EXCLUSIVE;
  256. return;
  257. }
  258. case NL80211_IFTYPE_STATION:
  259. case NL80211_IFTYPE_P2P_CLIENT:
  260. if (wdev->current_bss) {
  261. *chan = wdev->current_bss->pub.channel;
  262. *chanmode = CHAN_MODE_SHARED;
  263. return;
  264. }
  265. break;
  266. case NL80211_IFTYPE_AP:
  267. case NL80211_IFTYPE_P2P_GO:
  268. if (wdev->beacon_interval) {
  269. *chan = wdev->channel;
  270. *chanmode = CHAN_MODE_SHARED;
  271. }
  272. return;
  273. case NL80211_IFTYPE_MESH_POINT:
  274. if (wdev->mesh_id_len) {
  275. *chan = wdev->channel;
  276. *chanmode = CHAN_MODE_SHARED;
  277. }
  278. return;
  279. case NL80211_IFTYPE_MONITOR:
  280. case NL80211_IFTYPE_AP_VLAN:
  281. case NL80211_IFTYPE_WDS:
  282. /* these interface types don't really have a channel */
  283. return;
  284. case NL80211_IFTYPE_P2P_DEVICE:
  285. if (wdev->wiphy->features &
  286. NL80211_FEATURE_P2P_DEVICE_NEEDS_CHANNEL)
  287. *chanmode = CHAN_MODE_EXCLUSIVE;
  288. return;
  289. case NL80211_IFTYPE_UNSPECIFIED:
  290. case NUM_NL80211_IFTYPES:
  291. WARN_ON(1);
  292. }
  293. return;
  294. }