chan.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. trace_cfg80211_can_beacon_sec_chan(wiphy, chan, channel_type);
  46. switch (channel_type) {
  47. case NL80211_CHAN_HT40PLUS:
  48. diff = 20;
  49. break;
  50. case NL80211_CHAN_HT40MINUS:
  51. diff = -20;
  52. break;
  53. default:
  54. trace_cfg80211_return_bool(true);
  55. return true;
  56. }
  57. sec_chan = ieee80211_get_channel(wiphy, chan->center_freq + diff);
  58. if (!sec_chan) {
  59. trace_cfg80211_return_bool(false);
  60. return false;
  61. }
  62. /* we'll need a DFS capability later */
  63. if (sec_chan->flags & (IEEE80211_CHAN_DISABLED |
  64. IEEE80211_CHAN_PASSIVE_SCAN |
  65. IEEE80211_CHAN_NO_IBSS |
  66. IEEE80211_CHAN_RADAR)) {
  67. trace_cfg80211_return_bool(false);
  68. return false;
  69. }
  70. trace_cfg80211_return_bool(true);
  71. return true;
  72. }
  73. EXPORT_SYMBOL(cfg80211_can_beacon_sec_chan);
  74. int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev,
  75. int freq, enum nl80211_channel_type chantype)
  76. {
  77. struct ieee80211_channel *chan;
  78. if (!rdev->ops->set_monitor_channel)
  79. return -EOPNOTSUPP;
  80. if (!cfg80211_has_monitors_only(rdev))
  81. return -EBUSY;
  82. chan = rdev_freq_to_chan(rdev, freq, chantype);
  83. if (!chan)
  84. return -EINVAL;
  85. return rdev_set_monitor_channel(rdev, chan, chantype);
  86. }
  87. void
  88. cfg80211_get_chan_state(struct wireless_dev *wdev,
  89. struct ieee80211_channel **chan,
  90. enum cfg80211_chan_mode *chanmode)
  91. {
  92. *chan = NULL;
  93. *chanmode = CHAN_MODE_UNDEFINED;
  94. ASSERT_WDEV_LOCK(wdev);
  95. if (wdev->netdev && !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. if (wdev->beacon_interval) {
  117. *chan = wdev->channel;
  118. *chanmode = CHAN_MODE_SHARED;
  119. }
  120. return;
  121. case NL80211_IFTYPE_MESH_POINT:
  122. if (wdev->mesh_id_len) {
  123. *chan = wdev->channel;
  124. *chanmode = CHAN_MODE_SHARED;
  125. }
  126. return;
  127. case NL80211_IFTYPE_MONITOR:
  128. case NL80211_IFTYPE_AP_VLAN:
  129. case NL80211_IFTYPE_WDS:
  130. /* these interface types don't really have a channel */
  131. return;
  132. case NL80211_IFTYPE_P2P_DEVICE:
  133. if (wdev->wiphy->features &
  134. NL80211_FEATURE_P2P_DEVICE_NEEDS_CHANNEL)
  135. *chanmode = CHAN_MODE_EXCLUSIVE;
  136. return;
  137. case NL80211_IFTYPE_UNSPECIFIED:
  138. case NUM_NL80211_IFTYPES:
  139. WARN_ON(1);
  140. }
  141. return;
  142. }