chan.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 wireless_dev *wdev,
  87. struct ieee80211_channel **chan,
  88. enum cfg80211_chan_mode *chanmode)
  89. {
  90. *chan = NULL;
  91. *chanmode = CHAN_MODE_UNDEFINED;
  92. ASSERT_WDEV_LOCK(wdev);
  93. if (!netif_running(wdev->netdev))
  94. return;
  95. switch (wdev->iftype) {
  96. case NL80211_IFTYPE_ADHOC:
  97. if (wdev->current_bss) {
  98. *chan = wdev->current_bss->pub.channel;
  99. *chanmode = wdev->ibss_fixed
  100. ? CHAN_MODE_SHARED
  101. : CHAN_MODE_EXCLUSIVE;
  102. return;
  103. }
  104. case NL80211_IFTYPE_STATION:
  105. case NL80211_IFTYPE_P2P_CLIENT:
  106. if (wdev->current_bss) {
  107. *chan = wdev->current_bss->pub.channel;
  108. *chanmode = CHAN_MODE_SHARED;
  109. return;
  110. }
  111. break;
  112. case NL80211_IFTYPE_AP:
  113. case NL80211_IFTYPE_P2P_GO:
  114. case NL80211_IFTYPE_MESH_POINT:
  115. *chan = wdev->channel;
  116. *chanmode = CHAN_MODE_SHARED;
  117. return;
  118. case NL80211_IFTYPE_MONITOR:
  119. case NL80211_IFTYPE_AP_VLAN:
  120. case NL80211_IFTYPE_WDS:
  121. /* these interface types don't really have a channel */
  122. return;
  123. case NL80211_IFTYPE_UNSPECIFIED:
  124. case NUM_NL80211_IFTYPES:
  125. WARN_ON(1);
  126. }
  127. return;
  128. }