blob: 48febd2160ba488a7b8fccea612de1ae76b2c3fa [file] [log] [blame]
Johannes Berg59bbb6f2009-08-07 17:22:35 +02001/*
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 Simon54858ee5b2011-11-30 16:56:32 +01009#include <linux/export.h>
Johannes Berg59bbb6f2009-08-07 17:22:35 +020010#include <net/cfg80211.h>
11#include "core.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030012#include "rdev-ops.h"
Johannes Berg59bbb6f2009-08-07 17:22:35 +020013
14struct ieee80211_channel *
Jouni Malinen9588bbd2009-12-23 13:15:41 +010015rdev_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 Schaaaed8e1f2010-05-17 17:30:59 +020040 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 Malinen9588bbd2009-12-23 13:15:41 +010043 return NULL;
44 }
45
46 return chan;
47}
48
Johannes Berg294a20e2012-05-10 21:25:23 +020049bool cfg80211_can_beacon_sec_chan(struct wiphy *wiphy,
Alexander Simon54858ee5b2011-11-30 16:56:32 +010050 struct ieee80211_channel *chan,
51 enum nl80211_channel_type channel_type)
Luis R. Rodriguez9236d832010-11-12 16:31:23 -080052{
53 struct ieee80211_channel *sec_chan;
54 int diff;
55
Beni Lev4ee3e062012-08-27 12:49:39 +030056 trace_cfg80211_can_beacon_sec_chan(wiphy, chan, channel_type);
57
Luis R. Rodriguez9236d832010-11-12 16:31:23 -080058 switch (channel_type) {
59 case NL80211_CHAN_HT40PLUS:
60 diff = 20;
Mark Mentovai09a02fd2010-11-17 16:34:37 -050061 break;
Luis R. Rodriguez9236d832010-11-12 16:31:23 -080062 case NL80211_CHAN_HT40MINUS:
63 diff = -20;
Mark Mentovai09a02fd2010-11-17 16:34:37 -050064 break;
Luis R. Rodriguez9236d832010-11-12 16:31:23 -080065 default:
Beni Lev4ee3e062012-08-27 12:49:39 +030066 trace_cfg80211_return_bool(true);
Johannes Bergd58e7e32012-05-16 23:50:17 +020067 return true;
Luis R. Rodriguez9236d832010-11-12 16:31:23 -080068 }
69
70 sec_chan = ieee80211_get_channel(wiphy, chan->center_freq + diff);
Beni Lev4ee3e062012-08-27 12:49:39 +030071 if (!sec_chan) {
72 trace_cfg80211_return_bool(false);
Luis R. Rodriguez9236d832010-11-12 16:31:23 -080073 return false;
Beni Lev4ee3e062012-08-27 12:49:39 +030074 }
Luis R. Rodriguez9236d832010-11-12 16:31:23 -080075
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 Lev4ee3e062012-08-27 12:49:39 +030080 IEEE80211_CHAN_RADAR)) {
81 trace_cfg80211_return_bool(false);
Luis R. Rodriguez9236d832010-11-12 16:31:23 -080082 return false;
Beni Lev4ee3e062012-08-27 12:49:39 +030083 }
84 trace_cfg80211_return_bool(true);
Luis R. Rodriguez9236d832010-11-12 16:31:23 -080085 return true;
86}
Alexander Simon54858ee5b2011-11-30 16:56:32 +010087EXPORT_SYMBOL(cfg80211_can_beacon_sec_chan);
Luis R. Rodriguez9236d832010-11-12 16:31:23 -080088
Johannes Berge8c9bd52012-06-06 08:18:22 +020089int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev,
90 int freq, enum nl80211_channel_type chantype)
Johannes Berg59bbb6f2009-08-07 17:22:35 +020091{
92 struct ieee80211_channel *chan;
Johannes Berg59bbb6f2009-08-07 17:22:35 +020093
Johannes Berge8c9bd52012-06-06 08:18:22 +020094 if (!rdev->ops->set_monitor_channel)
Johannes Berg59bbb6f2009-08-07 17:22:35 +020095 return -EOPNOTSUPP;
Michal Kazior4f03c1e2012-06-29 12:47:03 +020096 if (!cfg80211_has_monitors_only(rdev))
97 return -EBUSY;
Johannes Berg59bbb6f2009-08-07 17:22:35 +020098
Johannes Berge8c9bd52012-06-06 08:18:22 +020099 chan = rdev_freq_to_chan(rdev, freq, chantype);
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100100 if (!chan)
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200101 return -EINVAL;
102
Hila Gonene35e4d22012-06-27 17:19:42 +0300103 return rdev_set_monitor_channel(rdev, chan, chantype);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200104}
Michal Kazior26ab9a02012-06-29 12:47:00 +0200105
106void
Johannes Berg8e95ea42012-07-10 19:39:02 +0200107cfg80211_get_chan_state(struct wireless_dev *wdev,
Michal Kazior26ab9a02012-06-29 12:47:00 +0200108 struct ieee80211_channel **chan,
109 enum cfg80211_chan_mode *chanmode)
110{
111 *chan = NULL;
112 *chanmode = CHAN_MODE_UNDEFINED;
113
Michal Kazior26ab9a02012-06-29 12:47:00 +0200114 ASSERT_WDEV_LOCK(wdev);
115
Johannes Berg98104fde2012-06-16 00:19:54 +0200116 if (wdev->netdev && !netif_running(wdev->netdev))
Michal Kazior26ab9a02012-06-29 12:47:00 +0200117 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 Fietkauf53594a2012-07-12 16:10:02 +0200138 if (wdev->beacon_interval) {
139 *chan = wdev->channel;
140 *chanmode = CHAN_MODE_SHARED;
141 }
142 return;
Michal Kazior26ab9a02012-06-29 12:47:00 +0200143 case NL80211_IFTYPE_MESH_POINT:
Felix Fietkauf53594a2012-07-12 16:10:02 +0200144 if (wdev->mesh_id_len) {
145 *chan = wdev->channel;
146 *chanmode = CHAN_MODE_SHARED;
147 }
Michal Kazior26ab9a02012-06-29 12:47:00 +0200148 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 Berg98104fde2012-06-16 00:19:54 +0200154 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 Kazior26ab9a02012-06-29 12:47:00 +0200159 case NL80211_IFTYPE_UNSPECIFIED:
160 case NUM_NL80211_IFTYPES:
161 WARN_ON(1);
162 }
163
164 return;
165}