blob: 396373f3ec260b96a178dce7e72c239cf78bdc51 [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 Simon54858ee2011-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
Johannes Berg3d9d1d62012-11-08 23:14:50 +010014void cfg80211_chandef_create(struct cfg80211_chan_def *chandef,
15 struct ieee80211_channel *chan,
16 enum nl80211_channel_type chan_type)
17{
18 if (WARN_ON(!chan))
19 return;
20
21 chandef->chan = chan;
22 chandef->center_freq2 = 0;
23
24 switch (chan_type) {
25 case NL80211_CHAN_NO_HT:
26 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
27 chandef->center_freq1 = chan->center_freq;
28 break;
29 case NL80211_CHAN_HT20:
30 chandef->width = NL80211_CHAN_WIDTH_20;
31 chandef->center_freq1 = chan->center_freq;
32 break;
33 case NL80211_CHAN_HT40PLUS:
34 chandef->width = NL80211_CHAN_WIDTH_40;
35 chandef->center_freq1 = chan->center_freq + 10;
36 break;
37 case NL80211_CHAN_HT40MINUS:
38 chandef->width = NL80211_CHAN_WIDTH_40;
39 chandef->center_freq1 = chan->center_freq - 10;
40 break;
41 default:
42 WARN_ON(1);
43 }
44}
45EXPORT_SYMBOL(cfg80211_chandef_create);
46
Johannes Berg9f5e8f62012-11-22 16:59:45 +010047bool cfg80211_chandef_valid(const struct cfg80211_chan_def *chandef)
Johannes Berg3d9d1d62012-11-08 23:14:50 +010048{
49 u32 control_freq;
50
51 if (!chandef->chan)
52 return false;
53
54 control_freq = chandef->chan->center_freq;
55
56 switch (chandef->width) {
57 case NL80211_CHAN_WIDTH_20:
58 case NL80211_CHAN_WIDTH_20_NOHT:
59 if (chandef->center_freq1 != control_freq)
60 return false;
61 if (chandef->center_freq2)
62 return false;
63 break;
64 case NL80211_CHAN_WIDTH_40:
65 if (chandef->center_freq1 != control_freq + 10 &&
66 chandef->center_freq1 != control_freq - 10)
67 return false;
68 if (chandef->center_freq2)
69 return false;
70 break;
71 case NL80211_CHAN_WIDTH_80P80:
72 if (chandef->center_freq1 != control_freq + 30 &&
73 chandef->center_freq1 != control_freq + 10 &&
74 chandef->center_freq1 != control_freq - 10 &&
75 chandef->center_freq1 != control_freq - 30)
76 return false;
77 if (!chandef->center_freq2)
78 return false;
Johannes Berg9cab3152012-12-14 00:19:08 +010079 /* adjacent is not allowed -- that's a 160 MHz channel */
80 if (chandef->center_freq1 - chandef->center_freq2 == 80 ||
81 chandef->center_freq2 - chandef->center_freq1 == 80)
82 return false;
Johannes Berg3d9d1d62012-11-08 23:14:50 +010083 break;
84 case NL80211_CHAN_WIDTH_80:
85 if (chandef->center_freq1 != control_freq + 30 &&
86 chandef->center_freq1 != control_freq + 10 &&
87 chandef->center_freq1 != control_freq - 10 &&
88 chandef->center_freq1 != control_freq - 30)
89 return false;
90 if (chandef->center_freq2)
91 return false;
92 break;
93 case NL80211_CHAN_WIDTH_160:
94 if (chandef->center_freq1 != control_freq + 70 &&
95 chandef->center_freq1 != control_freq + 50 &&
96 chandef->center_freq1 != control_freq + 30 &&
97 chandef->center_freq1 != control_freq + 10 &&
98 chandef->center_freq1 != control_freq - 10 &&
99 chandef->center_freq1 != control_freq - 30 &&
100 chandef->center_freq1 != control_freq - 50 &&
101 chandef->center_freq1 != control_freq - 70)
102 return false;
103 if (chandef->center_freq2)
104 return false;
105 break;
106 default:
107 return false;
108 }
109
110 return true;
111}
Johannes Berg9f5e8f62012-11-22 16:59:45 +0100112EXPORT_SYMBOL(cfg80211_chandef_valid);
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100113
114static void chandef_primary_freqs(const struct cfg80211_chan_def *c,
115 int *pri40, int *pri80)
116{
117 int tmp;
118
119 switch (c->width) {
120 case NL80211_CHAN_WIDTH_40:
121 *pri40 = c->center_freq1;
122 *pri80 = 0;
123 break;
124 case NL80211_CHAN_WIDTH_80:
125 case NL80211_CHAN_WIDTH_80P80:
126 *pri80 = c->center_freq1;
127 /* n_P20 */
128 tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
129 /* n_P40 */
130 tmp /= 2;
131 /* freq_P40 */
132 *pri40 = c->center_freq1 - 20 + 40 * tmp;
133 break;
134 case NL80211_CHAN_WIDTH_160:
135 /* n_P20 */
136 tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
137 /* n_P40 */
138 tmp /= 2;
139 /* freq_P40 */
140 *pri40 = c->center_freq1 - 60 + 40 * tmp;
141 /* n_P80 */
142 tmp /= 2;
143 *pri80 = c->center_freq1 - 40 + 80 * tmp;
144 break;
145 default:
146 WARN_ON_ONCE(1);
147 }
148}
149
150const struct cfg80211_chan_def *
151cfg80211_chandef_compatible(const struct cfg80211_chan_def *c1,
152 const struct cfg80211_chan_def *c2)
153{
154 u32 c1_pri40, c1_pri80, c2_pri40, c2_pri80;
155
156 /* If they are identical, return */
157 if (cfg80211_chandef_identical(c1, c2))
158 return c1;
159
160 /* otherwise, must have same control channel */
161 if (c1->chan != c2->chan)
162 return NULL;
163
164 /*
165 * If they have the same width, but aren't identical,
166 * then they can't be compatible.
167 */
168 if (c1->width == c2->width)
169 return NULL;
170
171 if (c1->width == NL80211_CHAN_WIDTH_20_NOHT ||
172 c1->width == NL80211_CHAN_WIDTH_20)
173 return c2;
174
175 if (c2->width == NL80211_CHAN_WIDTH_20_NOHT ||
176 c2->width == NL80211_CHAN_WIDTH_20)
177 return c1;
178
179 chandef_primary_freqs(c1, &c1_pri40, &c1_pri80);
180 chandef_primary_freqs(c2, &c2_pri40, &c2_pri80);
181
182 if (c1_pri40 != c2_pri40)
183 return NULL;
184
185 WARN_ON(!c1_pri80 && !c2_pri80);
186 if (c1_pri80 && c2_pri80 && c1_pri80 != c2_pri80)
187 return NULL;
188
189 if (c1->width > c2->width)
190 return c1;
191 return c2;
192}
193EXPORT_SYMBOL(cfg80211_chandef_compatible);
194
Johannes Berg9f5e8f62012-11-22 16:59:45 +0100195static bool cfg80211_secondary_chans_ok(struct wiphy *wiphy,
196 u32 center_freq, u32 bandwidth,
197 u32 prohibited_flags)
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100198{
199 struct ieee80211_channel *c;
200 u32 freq;
201
202 for (freq = center_freq - bandwidth/2 + 10;
203 freq <= center_freq + bandwidth/2 - 10;
204 freq += 20) {
205 c = ieee80211_get_channel(wiphy, freq);
206 if (!c || c->flags & prohibited_flags)
207 return false;
208 }
209
210 return true;
211}
212
Johannes Berg9f5e8f62012-11-22 16:59:45 +0100213bool cfg80211_chandef_usable(struct wiphy *wiphy,
214 const struct cfg80211_chan_def *chandef,
215 u32 prohibited_flags)
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100216{
Johannes Berg9f5e8f62012-11-22 16:59:45 +0100217 struct ieee80211_sta_ht_cap *ht_cap;
218 struct ieee80211_sta_vht_cap *vht_cap;
219 u32 width, control_freq;
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100220
Johannes Berg9f5e8f62012-11-22 16:59:45 +0100221 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100222 return false;
Johannes Berg9f5e8f62012-11-22 16:59:45 +0100223
224 ht_cap = &wiphy->bands[chandef->chan->band]->ht_cap;
225 vht_cap = &wiphy->bands[chandef->chan->band]->vht_cap;
226
227 control_freq = chandef->chan->center_freq;
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100228
229 switch (chandef->width) {
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100230 case NL80211_CHAN_WIDTH_20:
Johannes Berg9f5e8f62012-11-22 16:59:45 +0100231 if (!ht_cap->ht_supported)
232 return false;
233 case NL80211_CHAN_WIDTH_20_NOHT:
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100234 width = 20;
Mark Mentovai09a02fd2010-11-17 16:34:37 -0500235 break;
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100236 case NL80211_CHAN_WIDTH_40:
237 width = 40;
Johannes Berg9f5e8f62012-11-22 16:59:45 +0100238 if (!ht_cap->ht_supported)
239 return false;
240 if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
241 ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT)
242 return false;
243 if (chandef->center_freq1 < control_freq &&
244 chandef->chan->flags & IEEE80211_CHAN_NO_HT40MINUS)
245 return false;
246 if (chandef->center_freq1 > control_freq &&
247 chandef->chan->flags & IEEE80211_CHAN_NO_HT40PLUS)
248 return false;
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100249 break;
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100250 case NL80211_CHAN_WIDTH_80P80:
Johannes Berg9f5e8f62012-11-22 16:59:45 +0100251 if (!(vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ))
252 return false;
253 case NL80211_CHAN_WIDTH_80:
254 if (!vht_cap->vht_supported)
255 return false;
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100256 width = 80;
257 break;
258 case NL80211_CHAN_WIDTH_160:
Johannes Berg9f5e8f62012-11-22 16:59:45 +0100259 if (!vht_cap->vht_supported)
260 return false;
261 if (!(vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ))
262 return false;
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100263 width = 160;
Mark Mentovai09a02fd2010-11-17 16:34:37 -0500264 break;
Luis R. Rodriguez9236d832010-11-12 16:31:23 -0800265 default:
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100266 WARN_ON_ONCE(1);
Luis R. Rodriguez9236d832010-11-12 16:31:23 -0800267 return false;
Beni Lev4ee3e062012-08-27 12:49:39 +0300268 }
Luis R. Rodriguez9236d832010-11-12 16:31:23 -0800269
Johannes Berg9f5e8f62012-11-22 16:59:45 +0100270 /* TODO: missing regulatory check on 80/160 bandwidth */
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100271
Johannes Berga6662db2012-12-04 20:49:42 +0100272 if (width > 20)
273 prohibited_flags |= IEEE80211_CHAN_NO_OFDM;
274
Johannes Berg9f5e8f62012-11-22 16:59:45 +0100275 if (!cfg80211_secondary_chans_ok(wiphy, chandef->center_freq1,
276 width, prohibited_flags))
277 return false;
278
279 if (!chandef->center_freq2)
280 return true;
281 return cfg80211_secondary_chans_ok(wiphy, chandef->center_freq2,
282 width, prohibited_flags);
283}
284EXPORT_SYMBOL(cfg80211_chandef_usable);
285
286bool cfg80211_reg_can_beacon(struct wiphy *wiphy,
287 struct cfg80211_chan_def *chandef)
288{
289 bool res;
290
291 trace_cfg80211_reg_can_beacon(wiphy, chandef);
292
293 res = cfg80211_chandef_usable(wiphy, chandef,
294 IEEE80211_CHAN_DISABLED |
295 IEEE80211_CHAN_PASSIVE_SCAN |
296 IEEE80211_CHAN_NO_IBSS |
297 IEEE80211_CHAN_RADAR);
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100298
299 trace_cfg80211_return_bool(res);
300 return res;
Luis R. Rodriguez9236d832010-11-12 16:31:23 -0800301}
Johannes Berg683b6d32012-11-08 21:25:48 +0100302EXPORT_SYMBOL(cfg80211_reg_can_beacon);
Luis R. Rodriguez9236d832010-11-12 16:31:23 -0800303
Johannes Berge8c9bd52012-06-06 08:18:22 +0200304int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev,
Johannes Berg683b6d32012-11-08 21:25:48 +0100305 struct cfg80211_chan_def *chandef)
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200306{
Johannes Berge8c9bd52012-06-06 08:18:22 +0200307 if (!rdev->ops->set_monitor_channel)
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200308 return -EOPNOTSUPP;
Michal Kazior4f03c1e2012-06-29 12:47:03 +0200309 if (!cfg80211_has_monitors_only(rdev))
310 return -EBUSY;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200311
Johannes Berg683b6d32012-11-08 21:25:48 +0100312 return rdev_set_monitor_channel(rdev, chandef);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200313}
Michal Kazior26ab9a02012-06-29 12:47:00 +0200314
315void
Johannes Berg8e95ea42012-07-10 19:39:02 +0200316cfg80211_get_chan_state(struct wireless_dev *wdev,
Michal Kazior26ab9a02012-06-29 12:47:00 +0200317 struct ieee80211_channel **chan,
318 enum cfg80211_chan_mode *chanmode)
319{
320 *chan = NULL;
321 *chanmode = CHAN_MODE_UNDEFINED;
322
Michal Kazior26ab9a02012-06-29 12:47:00 +0200323 ASSERT_WDEV_LOCK(wdev);
324
Johannes Berg98104fde2012-06-16 00:19:54 +0200325 if (wdev->netdev && !netif_running(wdev->netdev))
Michal Kazior26ab9a02012-06-29 12:47:00 +0200326 return;
327
328 switch (wdev->iftype) {
329 case NL80211_IFTYPE_ADHOC:
330 if (wdev->current_bss) {
331 *chan = wdev->current_bss->pub.channel;
332 *chanmode = wdev->ibss_fixed
333 ? CHAN_MODE_SHARED
334 : CHAN_MODE_EXCLUSIVE;
335 return;
336 }
337 case NL80211_IFTYPE_STATION:
338 case NL80211_IFTYPE_P2P_CLIENT:
339 if (wdev->current_bss) {
340 *chan = wdev->current_bss->pub.channel;
341 *chanmode = CHAN_MODE_SHARED;
342 return;
343 }
344 break;
345 case NL80211_IFTYPE_AP:
346 case NL80211_IFTYPE_P2P_GO:
Felix Fietkauf53594a2012-07-12 16:10:02 +0200347 if (wdev->beacon_interval) {
348 *chan = wdev->channel;
349 *chanmode = CHAN_MODE_SHARED;
350 }
351 return;
Michal Kazior26ab9a02012-06-29 12:47:00 +0200352 case NL80211_IFTYPE_MESH_POINT:
Felix Fietkauf53594a2012-07-12 16:10:02 +0200353 if (wdev->mesh_id_len) {
354 *chan = wdev->channel;
355 *chanmode = CHAN_MODE_SHARED;
356 }
Michal Kazior26ab9a02012-06-29 12:47:00 +0200357 return;
358 case NL80211_IFTYPE_MONITOR:
359 case NL80211_IFTYPE_AP_VLAN:
360 case NL80211_IFTYPE_WDS:
361 /* these interface types don't really have a channel */
362 return;
Johannes Berg98104fde2012-06-16 00:19:54 +0200363 case NL80211_IFTYPE_P2P_DEVICE:
364 if (wdev->wiphy->features &
365 NL80211_FEATURE_P2P_DEVICE_NEEDS_CHANNEL)
366 *chanmode = CHAN_MODE_EXCLUSIVE;
367 return;
Michal Kazior26ab9a02012-06-29 12:47:00 +0200368 case NL80211_IFTYPE_UNSPECIFIED:
369 case NUM_NL80211_IFTYPES:
370 WARN_ON(1);
371 }
372
373 return;
374}