Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 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 | |
Alexander Simon | 54858ee5b | 2011-11-30 16:56:32 +0100 | [diff] [blame] | 9 | #include <linux/export.h> |
Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 10 | #include <net/cfg80211.h> |
| 11 | #include "core.h" |
Hila Gonen | e35e4d2 | 2012-06-27 17:19:42 +0300 | [diff] [blame] | 12 | #include "rdev-ops.h" |
Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 13 | |
| 14 | struct ieee80211_channel * |
Jouni Malinen | 9588bbd | 2009-12-23 13:15:41 +0100 | [diff] [blame] | 15 | rdev_freq_to_chan(struct cfg80211_registered_device *rdev, |
| 16 | int freq, enum nl80211_channel_type channel_type) |
| 17 | { |
| 18 | struct ieee80211_channel *chan; |
| 19 | struct ieee80211_sta_ht_cap *ht_cap; |
| 20 | |
| 21 | chan = ieee80211_get_channel(&rdev->wiphy, freq); |
| 22 | |
| 23 | /* Primary channel not allowed */ |
| 24 | if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) |
| 25 | return NULL; |
| 26 | |
| 27 | if (channel_type == NL80211_CHAN_HT40MINUS && |
| 28 | chan->flags & IEEE80211_CHAN_NO_HT40MINUS) |
| 29 | return NULL; |
| 30 | else if (channel_type == NL80211_CHAN_HT40PLUS && |
| 31 | chan->flags & IEEE80211_CHAN_NO_HT40PLUS) |
| 32 | return NULL; |
| 33 | |
| 34 | ht_cap = &rdev->wiphy.bands[chan->band]->ht_cap; |
| 35 | |
| 36 | if (channel_type != NL80211_CHAN_NO_HT) { |
| 37 | if (!ht_cap->ht_supported) |
| 38 | return NULL; |
| 39 | |
Helmut Schaa | aed8e1f | 2010-05-17 17:30:59 +0200 | [diff] [blame] | 40 | if (channel_type != NL80211_CHAN_HT20 && |
| 41 | (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) || |
| 42 | ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT)) |
Jouni Malinen | 9588bbd | 2009-12-23 13:15:41 +0100 | [diff] [blame] | 43 | return NULL; |
| 44 | } |
| 45 | |
| 46 | return chan; |
| 47 | } |
| 48 | |
Johannes Berg | 294a20e | 2012-05-10 21:25:23 +0200 | [diff] [blame] | 49 | bool cfg80211_can_beacon_sec_chan(struct wiphy *wiphy, |
Alexander Simon | 54858ee5b | 2011-11-30 16:56:32 +0100 | [diff] [blame] | 50 | struct ieee80211_channel *chan, |
| 51 | enum nl80211_channel_type channel_type) |
Luis R. Rodriguez | 9236d83 | 2010-11-12 16:31:23 -0800 | [diff] [blame] | 52 | { |
| 53 | struct ieee80211_channel *sec_chan; |
| 54 | int diff; |
| 55 | |
Beni Lev | 4ee3e06 | 2012-08-27 12:49:39 +0300 | [diff] [blame^] | 56 | trace_cfg80211_can_beacon_sec_chan(wiphy, chan, channel_type); |
| 57 | |
Luis R. Rodriguez | 9236d83 | 2010-11-12 16:31:23 -0800 | [diff] [blame] | 58 | switch (channel_type) { |
| 59 | case NL80211_CHAN_HT40PLUS: |
| 60 | diff = 20; |
Mark Mentovai | 09a02fd | 2010-11-17 16:34:37 -0500 | [diff] [blame] | 61 | break; |
Luis R. Rodriguez | 9236d83 | 2010-11-12 16:31:23 -0800 | [diff] [blame] | 62 | case NL80211_CHAN_HT40MINUS: |
| 63 | diff = -20; |
Mark Mentovai | 09a02fd | 2010-11-17 16:34:37 -0500 | [diff] [blame] | 64 | break; |
Luis R. Rodriguez | 9236d83 | 2010-11-12 16:31:23 -0800 | [diff] [blame] | 65 | default: |
Beni Lev | 4ee3e06 | 2012-08-27 12:49:39 +0300 | [diff] [blame^] | 66 | trace_cfg80211_return_bool(true); |
Johannes Berg | d58e7e3 | 2012-05-16 23:50:17 +0200 | [diff] [blame] | 67 | return true; |
Luis R. Rodriguez | 9236d83 | 2010-11-12 16:31:23 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | sec_chan = ieee80211_get_channel(wiphy, chan->center_freq + diff); |
Beni Lev | 4ee3e06 | 2012-08-27 12:49:39 +0300 | [diff] [blame^] | 71 | if (!sec_chan) { |
| 72 | trace_cfg80211_return_bool(false); |
Luis R. Rodriguez | 9236d83 | 2010-11-12 16:31:23 -0800 | [diff] [blame] | 73 | return false; |
Beni Lev | 4ee3e06 | 2012-08-27 12:49:39 +0300 | [diff] [blame^] | 74 | } |
Luis R. Rodriguez | 9236d83 | 2010-11-12 16:31:23 -0800 | [diff] [blame] | 75 | |
| 76 | /* we'll need a DFS capability later */ |
| 77 | if (sec_chan->flags & (IEEE80211_CHAN_DISABLED | |
| 78 | IEEE80211_CHAN_PASSIVE_SCAN | |
| 79 | IEEE80211_CHAN_NO_IBSS | |
Beni Lev | 4ee3e06 | 2012-08-27 12:49:39 +0300 | [diff] [blame^] | 80 | IEEE80211_CHAN_RADAR)) { |
| 81 | trace_cfg80211_return_bool(false); |
Luis R. Rodriguez | 9236d83 | 2010-11-12 16:31:23 -0800 | [diff] [blame] | 82 | return false; |
Beni Lev | 4ee3e06 | 2012-08-27 12:49:39 +0300 | [diff] [blame^] | 83 | } |
| 84 | trace_cfg80211_return_bool(true); |
Luis R. Rodriguez | 9236d83 | 2010-11-12 16:31:23 -0800 | [diff] [blame] | 85 | return true; |
| 86 | } |
Alexander Simon | 54858ee5b | 2011-11-30 16:56:32 +0100 | [diff] [blame] | 87 | EXPORT_SYMBOL(cfg80211_can_beacon_sec_chan); |
Luis R. Rodriguez | 9236d83 | 2010-11-12 16:31:23 -0800 | [diff] [blame] | 88 | |
Johannes Berg | e8c9bd5 | 2012-06-06 08:18:22 +0200 | [diff] [blame] | 89 | int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev, |
| 90 | int freq, enum nl80211_channel_type chantype) |
Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 91 | { |
| 92 | struct ieee80211_channel *chan; |
Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 93 | |
Johannes Berg | e8c9bd5 | 2012-06-06 08:18:22 +0200 | [diff] [blame] | 94 | if (!rdev->ops->set_monitor_channel) |
Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 95 | return -EOPNOTSUPP; |
Michal Kazior | 4f03c1e | 2012-06-29 12:47:03 +0200 | [diff] [blame] | 96 | if (!cfg80211_has_monitors_only(rdev)) |
| 97 | return -EBUSY; |
Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 98 | |
Johannes Berg | e8c9bd5 | 2012-06-06 08:18:22 +0200 | [diff] [blame] | 99 | chan = rdev_freq_to_chan(rdev, freq, chantype); |
Jouni Malinen | 9588bbd | 2009-12-23 13:15:41 +0100 | [diff] [blame] | 100 | if (!chan) |
Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 101 | return -EINVAL; |
| 102 | |
Hila Gonen | e35e4d2 | 2012-06-27 17:19:42 +0300 | [diff] [blame] | 103 | return rdev_set_monitor_channel(rdev, chan, chantype); |
Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 104 | } |
Michal Kazior | 26ab9a0 | 2012-06-29 12:47:00 +0200 | [diff] [blame] | 105 | |
| 106 | void |
Johannes Berg | 8e95ea4 | 2012-07-10 19:39:02 +0200 | [diff] [blame] | 107 | cfg80211_get_chan_state(struct wireless_dev *wdev, |
Michal Kazior | 26ab9a0 | 2012-06-29 12:47:00 +0200 | [diff] [blame] | 108 | struct ieee80211_channel **chan, |
| 109 | enum cfg80211_chan_mode *chanmode) |
| 110 | { |
| 111 | *chan = NULL; |
| 112 | *chanmode = CHAN_MODE_UNDEFINED; |
| 113 | |
Michal Kazior | 26ab9a0 | 2012-06-29 12:47:00 +0200 | [diff] [blame] | 114 | ASSERT_WDEV_LOCK(wdev); |
| 115 | |
Johannes Berg | 98104fde | 2012-06-16 00:19:54 +0200 | [diff] [blame] | 116 | if (wdev->netdev && !netif_running(wdev->netdev)) |
Michal Kazior | 26ab9a0 | 2012-06-29 12:47:00 +0200 | [diff] [blame] | 117 | return; |
| 118 | |
| 119 | switch (wdev->iftype) { |
| 120 | case NL80211_IFTYPE_ADHOC: |
| 121 | if (wdev->current_bss) { |
| 122 | *chan = wdev->current_bss->pub.channel; |
| 123 | *chanmode = wdev->ibss_fixed |
| 124 | ? CHAN_MODE_SHARED |
| 125 | : CHAN_MODE_EXCLUSIVE; |
| 126 | return; |
| 127 | } |
| 128 | case NL80211_IFTYPE_STATION: |
| 129 | case NL80211_IFTYPE_P2P_CLIENT: |
| 130 | if (wdev->current_bss) { |
| 131 | *chan = wdev->current_bss->pub.channel; |
| 132 | *chanmode = CHAN_MODE_SHARED; |
| 133 | return; |
| 134 | } |
| 135 | break; |
| 136 | case NL80211_IFTYPE_AP: |
| 137 | case NL80211_IFTYPE_P2P_GO: |
Felix Fietkau | f53594a | 2012-07-12 16:10:02 +0200 | [diff] [blame] | 138 | if (wdev->beacon_interval) { |
| 139 | *chan = wdev->channel; |
| 140 | *chanmode = CHAN_MODE_SHARED; |
| 141 | } |
| 142 | return; |
Michal Kazior | 26ab9a0 | 2012-06-29 12:47:00 +0200 | [diff] [blame] | 143 | case NL80211_IFTYPE_MESH_POINT: |
Felix Fietkau | f53594a | 2012-07-12 16:10:02 +0200 | [diff] [blame] | 144 | if (wdev->mesh_id_len) { |
| 145 | *chan = wdev->channel; |
| 146 | *chanmode = CHAN_MODE_SHARED; |
| 147 | } |
Michal Kazior | 26ab9a0 | 2012-06-29 12:47:00 +0200 | [diff] [blame] | 148 | return; |
| 149 | case NL80211_IFTYPE_MONITOR: |
| 150 | case NL80211_IFTYPE_AP_VLAN: |
| 151 | case NL80211_IFTYPE_WDS: |
| 152 | /* these interface types don't really have a channel */ |
| 153 | return; |
Johannes Berg | 98104fde | 2012-06-16 00:19:54 +0200 | [diff] [blame] | 154 | case NL80211_IFTYPE_P2P_DEVICE: |
| 155 | if (wdev->wiphy->features & |
| 156 | NL80211_FEATURE_P2P_DEVICE_NEEDS_CHANNEL) |
| 157 | *chanmode = CHAN_MODE_EXCLUSIVE; |
| 158 | return; |
Michal Kazior | 26ab9a0 | 2012-06-29 12:47:00 +0200 | [diff] [blame] | 159 | case NL80211_IFTYPE_UNSPECIFIED: |
| 160 | case NUM_NL80211_IFTYPES: |
| 161 | WARN_ON(1); |
| 162 | } |
| 163 | |
| 164 | return; |
| 165 | } |