blob: 019401b0b5e3029a994ed70233b276f8cbc4a6ef [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"
12
13struct ieee80211_channel *
Jouni Malinen9588bbd2009-12-23 13:15:41 +010014rdev_freq_to_chan(struct cfg80211_registered_device *rdev,
15 int freq, enum nl80211_channel_type channel_type)
16{
17 struct ieee80211_channel *chan;
18 struct ieee80211_sta_ht_cap *ht_cap;
19
20 chan = ieee80211_get_channel(&rdev->wiphy, freq);
21
22 /* Primary channel not allowed */
23 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
24 return NULL;
25
26 if (channel_type == NL80211_CHAN_HT40MINUS &&
27 chan->flags & IEEE80211_CHAN_NO_HT40MINUS)
28 return NULL;
29 else if (channel_type == NL80211_CHAN_HT40PLUS &&
30 chan->flags & IEEE80211_CHAN_NO_HT40PLUS)
31 return NULL;
32
33 ht_cap = &rdev->wiphy.bands[chan->band]->ht_cap;
34
35 if (channel_type != NL80211_CHAN_NO_HT) {
36 if (!ht_cap->ht_supported)
37 return NULL;
38
Helmut Schaaaed8e1f2010-05-17 17:30:59 +020039 if (channel_type != NL80211_CHAN_HT20 &&
40 (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
41 ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT))
Jouni Malinen9588bbd2009-12-23 13:15:41 +010042 return NULL;
43 }
44
45 return chan;
46}
47
Johannes Berg294a20e2012-05-10 21:25:23 +020048bool cfg80211_can_beacon_sec_chan(struct wiphy *wiphy,
Alexander Simon54858ee5b2011-11-30 16:56:32 +010049 struct ieee80211_channel *chan,
50 enum nl80211_channel_type channel_type)
Luis R. Rodriguez9236d832010-11-12 16:31:23 -080051{
52 struct ieee80211_channel *sec_chan;
53 int diff;
54
55 switch (channel_type) {
56 case NL80211_CHAN_HT40PLUS:
57 diff = 20;
Mark Mentovai09a02fd2010-11-17 16:34:37 -050058 break;
Luis R. Rodriguez9236d832010-11-12 16:31:23 -080059 case NL80211_CHAN_HT40MINUS:
60 diff = -20;
Mark Mentovai09a02fd2010-11-17 16:34:37 -050061 break;
Luis R. Rodriguez9236d832010-11-12 16:31:23 -080062 default:
Johannes Bergd58e7e32012-05-16 23:50:17 +020063 return true;
Luis R. Rodriguez9236d832010-11-12 16:31:23 -080064 }
65
66 sec_chan = ieee80211_get_channel(wiphy, chan->center_freq + diff);
67 if (!sec_chan)
68 return false;
69
70 /* we'll need a DFS capability later */
71 if (sec_chan->flags & (IEEE80211_CHAN_DISABLED |
72 IEEE80211_CHAN_PASSIVE_SCAN |
73 IEEE80211_CHAN_NO_IBSS |
74 IEEE80211_CHAN_RADAR))
75 return false;
76
77 return true;
78}
Alexander Simon54858ee5b2011-11-30 16:56:32 +010079EXPORT_SYMBOL(cfg80211_can_beacon_sec_chan);
Luis R. Rodriguez9236d832010-11-12 16:31:23 -080080
Johannes Berge8c9bd52012-06-06 08:18:22 +020081int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev,
82 int freq, enum nl80211_channel_type chantype)
Johannes Berg59bbb6f2009-08-07 17:22:35 +020083{
84 struct ieee80211_channel *chan;
Johannes Berg59bbb6f2009-08-07 17:22:35 +020085
Johannes Berge8c9bd52012-06-06 08:18:22 +020086 if (!rdev->ops->set_monitor_channel)
Johannes Berg59bbb6f2009-08-07 17:22:35 +020087 return -EOPNOTSUPP;
Michal Kazior4f03c1e2012-06-29 12:47:03 +020088 if (!cfg80211_has_monitors_only(rdev))
89 return -EBUSY;
Johannes Berg59bbb6f2009-08-07 17:22:35 +020090
Johannes Berge8c9bd52012-06-06 08:18:22 +020091 chan = rdev_freq_to_chan(rdev, freq, chantype);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010092 if (!chan)
Johannes Berg59bbb6f2009-08-07 17:22:35 +020093 return -EINVAL;
94
Johannes Berge8c9bd52012-06-06 08:18:22 +020095 return rdev->ops->set_monitor_channel(&rdev->wiphy, chan, chantype);
Johannes Berg59bbb6f2009-08-07 17:22:35 +020096}
Michal Kazior26ab9a02012-06-29 12:47:00 +020097
98void
99cfg80211_get_chan_state(struct cfg80211_registered_device *rdev,
100 struct wireless_dev *wdev,
101 struct ieee80211_channel **chan,
102 enum cfg80211_chan_mode *chanmode)
103{
104 *chan = NULL;
105 *chanmode = CHAN_MODE_UNDEFINED;
106
107 ASSERT_RDEV_LOCK(rdev);
108 ASSERT_WDEV_LOCK(wdev);
109
110 if (!netif_running(wdev->netdev))
111 return;
112
113 switch (wdev->iftype) {
114 case NL80211_IFTYPE_ADHOC:
115 if (wdev->current_bss) {
116 *chan = wdev->current_bss->pub.channel;
117 *chanmode = wdev->ibss_fixed
118 ? CHAN_MODE_SHARED
119 : CHAN_MODE_EXCLUSIVE;
120 return;
121 }
122 case NL80211_IFTYPE_STATION:
123 case NL80211_IFTYPE_P2P_CLIENT:
124 if (wdev->current_bss) {
125 *chan = wdev->current_bss->pub.channel;
126 *chanmode = CHAN_MODE_SHARED;
127 return;
128 }
129 break;
130 case NL80211_IFTYPE_AP:
131 case NL80211_IFTYPE_P2P_GO:
132 case NL80211_IFTYPE_MESH_POINT:
133 *chan = wdev->channel;
134 *chanmode = CHAN_MODE_SHARED;
135 return;
136 case NL80211_IFTYPE_MONITOR:
137 case NL80211_IFTYPE_AP_VLAN:
138 case NL80211_IFTYPE_WDS:
139 /* these interface types don't really have a channel */
140 return;
141 case NL80211_IFTYPE_UNSPECIFIED:
142 case NUM_NL80211_IFTYPES:
143 WARN_ON(1);
144 }
145
146 return;
147}