chan.c 3.7 KB

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