chan.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. if (!rdev->ops->set_monitor_channel)
  71. return -EOPNOTSUPP;
  72. if (!cfg80211_has_monitors_only(rdev))
  73. return -EBUSY;
  74. chan = rdev_freq_to_chan(rdev, freq, chantype);
  75. if (!chan)
  76. return -EINVAL;
  77. return rdev->ops->set_monitor_channel(&rdev->wiphy, chan, chantype);
  78. }
  79. void
  80. cfg80211_get_chan_state(struct wireless_dev *wdev,
  81. struct ieee80211_channel **chan,
  82. enum cfg80211_chan_mode *chanmode)
  83. {
  84. *chan = NULL;
  85. *chanmode = CHAN_MODE_UNDEFINED;
  86. ASSERT_WDEV_LOCK(wdev);
  87. if (wdev->netdev && !netif_running(wdev->netdev))
  88. return;
  89. switch (wdev->iftype) {
  90. case NL80211_IFTYPE_ADHOC:
  91. if (wdev->current_bss) {
  92. *chan = wdev->current_bss->pub.channel;
  93. *chanmode = wdev->ibss_fixed
  94. ? CHAN_MODE_SHARED
  95. : CHAN_MODE_EXCLUSIVE;
  96. return;
  97. }
  98. case NL80211_IFTYPE_STATION:
  99. case NL80211_IFTYPE_P2P_CLIENT:
  100. if (wdev->current_bss) {
  101. *chan = wdev->current_bss->pub.channel;
  102. *chanmode = CHAN_MODE_SHARED;
  103. return;
  104. }
  105. break;
  106. case NL80211_IFTYPE_AP:
  107. case NL80211_IFTYPE_P2P_GO:
  108. if (wdev->beacon_interval) {
  109. *chan = wdev->channel;
  110. *chanmode = CHAN_MODE_SHARED;
  111. }
  112. return;
  113. case NL80211_IFTYPE_MESH_POINT:
  114. if (wdev->mesh_id_len) {
  115. *chan = wdev->channel;
  116. *chanmode = CHAN_MODE_SHARED;
  117. }
  118. return;
  119. case NL80211_IFTYPE_MONITOR:
  120. case NL80211_IFTYPE_AP_VLAN:
  121. case NL80211_IFTYPE_WDS:
  122. /* these interface types don't really have a channel */
  123. return;
  124. case NL80211_IFTYPE_P2P_DEVICE:
  125. if (wdev->wiphy->features &
  126. NL80211_FEATURE_P2P_DEVICE_NEEDS_CHANNEL)
  127. *chanmode = CHAN_MODE_EXCLUSIVE;
  128. return;
  129. case NL80211_IFTYPE_UNSPECIFIED:
  130. case NUM_NL80211_IFTYPES:
  131. WARN_ON(1);
  132. }
  133. return;
  134. }