chan.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. struct ieee80211_channel *
  12. rdev_freq_to_chan(struct cfg80211_registered_device *rdev,
  13. int freq, enum nl80211_channel_type channel_type)
  14. {
  15. struct ieee80211_channel *chan;
  16. struct ieee80211_sta_ht_cap *ht_cap;
  17. chan = ieee80211_get_channel(&rdev->wiphy, freq);
  18. /* Primary channel not allowed */
  19. if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
  20. return NULL;
  21. if (channel_type == NL80211_CHAN_HT40MINUS &&
  22. chan->flags & IEEE80211_CHAN_NO_HT40MINUS)
  23. return NULL;
  24. else if (channel_type == NL80211_CHAN_HT40PLUS &&
  25. chan->flags & IEEE80211_CHAN_NO_HT40PLUS)
  26. return NULL;
  27. ht_cap = &rdev->wiphy.bands[chan->band]->ht_cap;
  28. if (channel_type != NL80211_CHAN_NO_HT) {
  29. if (!ht_cap->ht_supported)
  30. return NULL;
  31. if (channel_type != NL80211_CHAN_HT20 &&
  32. (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
  33. ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT))
  34. return NULL;
  35. }
  36. return chan;
  37. }
  38. bool cfg80211_can_beacon_sec_chan(struct wiphy *wiphy,
  39. struct ieee80211_channel *chan,
  40. enum nl80211_channel_type channel_type)
  41. {
  42. struct ieee80211_channel *sec_chan;
  43. int diff;
  44. switch (channel_type) {
  45. case NL80211_CHAN_HT40PLUS:
  46. diff = 20;
  47. break;
  48. case NL80211_CHAN_HT40MINUS:
  49. diff = -20;
  50. break;
  51. default:
  52. return true;
  53. }
  54. sec_chan = ieee80211_get_channel(wiphy, chan->center_freq + diff);
  55. if (!sec_chan)
  56. return false;
  57. /* we'll need a DFS capability later */
  58. if (sec_chan->flags & (IEEE80211_CHAN_DISABLED |
  59. IEEE80211_CHAN_PASSIVE_SCAN |
  60. IEEE80211_CHAN_NO_IBSS |
  61. IEEE80211_CHAN_RADAR))
  62. return false;
  63. return true;
  64. }
  65. EXPORT_SYMBOL(cfg80211_can_beacon_sec_chan);
  66. int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev,
  67. int freq, enum nl80211_channel_type chantype)
  68. {
  69. struct ieee80211_channel *chan;
  70. int err;
  71. if (!rdev->ops->set_monitor_channel)
  72. return -EOPNOTSUPP;
  73. if (!cfg80211_has_monitors_only(rdev))
  74. return -EBUSY;
  75. chan = rdev_freq_to_chan(rdev, freq, chantype);
  76. if (!chan)
  77. return -EINVAL;
  78. err = rdev->ops->set_monitor_channel(&rdev->wiphy, chan, chantype);
  79. if (!err) {
  80. rdev->monitor_channel = chan;
  81. rdev->monitor_channel_type = chantype;
  82. }
  83. return err;
  84. }
  85. void
  86. cfg80211_get_chan_state(struct cfg80211_registered_device *rdev,
  87. struct wireless_dev *wdev,
  88. struct ieee80211_channel **chan,
  89. enum cfg80211_chan_mode *chanmode)
  90. {
  91. *chan = NULL;
  92. *chanmode = CHAN_MODE_UNDEFINED;
  93. ASSERT_RDEV_LOCK(rdev);
  94. ASSERT_WDEV_LOCK(wdev);
  95. if (!netif_running(wdev->netdev))
  96. return;
  97. switch (wdev->iftype) {
  98. case NL80211_IFTYPE_ADHOC:
  99. if (wdev->current_bss) {
  100. *chan = wdev->current_bss->pub.channel;
  101. *chanmode = wdev->ibss_fixed
  102. ? CHAN_MODE_SHARED
  103. : CHAN_MODE_EXCLUSIVE;
  104. return;
  105. }
  106. case NL80211_IFTYPE_STATION:
  107. case NL80211_IFTYPE_P2P_CLIENT:
  108. if (wdev->current_bss) {
  109. *chan = wdev->current_bss->pub.channel;
  110. *chanmode = CHAN_MODE_SHARED;
  111. return;
  112. }
  113. break;
  114. case NL80211_IFTYPE_AP:
  115. case NL80211_IFTYPE_P2P_GO:
  116. case NL80211_IFTYPE_MESH_POINT:
  117. *chan = wdev->channel;
  118. *chanmode = CHAN_MODE_SHARED;
  119. return;
  120. case NL80211_IFTYPE_MONITOR:
  121. case NL80211_IFTYPE_AP_VLAN:
  122. case NL80211_IFTYPE_WDS:
  123. /* these interface types don't really have a channel */
  124. return;
  125. case NL80211_IFTYPE_UNSPECIFIED:
  126. case NUM_NL80211_IFTYPES:
  127. WARN_ON(1);
  128. }
  129. return;
  130. }