Commit | Line | Data |
---|---|---|
59bbb6f7 JB |
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 | ||
54858ee5 | 9 | #include <linux/export.h> |
59bbb6f7 JB |
10 | #include <net/cfg80211.h> |
11 | #include "core.h" | |
e35e4d28 | 12 | #include "rdev-ops.h" |
59bbb6f7 | 13 | |
3d9d1d66 JB |
14 | void cfg80211_chandef_create(struct cfg80211_chan_def *chandef, |
15 | struct ieee80211_channel *chan, | |
16 | enum nl80211_channel_type chan_type) | |
9236d838 | 17 | { |
3d9d1d66 JB |
18 | if (WARN_ON(!chan)) |
19 | return; | |
9236d838 | 20 | |
3d9d1d66 JB |
21 | chandef->chan = chan; |
22 | chandef->center_freq2 = 0; | |
4ee3e063 | 23 | |
3d9d1d66 JB |
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; | |
9236d838 | 33 | case NL80211_CHAN_HT40PLUS: |
3d9d1d66 JB |
34 | chandef->width = NL80211_CHAN_WIDTH_40; |
35 | chandef->center_freq1 = chan->center_freq + 10; | |
09a02fdb | 36 | break; |
9236d838 | 37 | case NL80211_CHAN_HT40MINUS: |
3d9d1d66 JB |
38 | chandef->width = NL80211_CHAN_WIDTH_40; |
39 | chandef->center_freq1 = chan->center_freq - 10; | |
09a02fdb | 40 | break; |
9236d838 | 41 | default: |
3d9d1d66 JB |
42 | WARN_ON(1); |
43 | } | |
44 | } | |
45 | EXPORT_SYMBOL(cfg80211_chandef_create); | |
46 | ||
9f5e8f6e | 47 | bool cfg80211_chandef_valid(const struct cfg80211_chan_def *chandef) |
3d9d1d66 JB |
48 | { |
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; | |
9cab3151 JB |
79 | /* 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; | |
3d9d1d66 JB |
83 | 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 | } | |
9f5e8f6e | 112 | EXPORT_SYMBOL(cfg80211_chandef_valid); |
3d9d1d66 JB |
113 | |
114 | static 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 | ||
150 | const struct cfg80211_chan_def * | |
151 | cfg80211_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 | } | |
193 | EXPORT_SYMBOL(cfg80211_chandef_compatible); | |
194 | ||
9f5e8f6e JB |
195 | static bool cfg80211_secondary_chans_ok(struct wiphy *wiphy, |
196 | u32 center_freq, u32 bandwidth, | |
197 | u32 prohibited_flags) | |
3d9d1d66 JB |
198 | { |
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; | |
9236d838 LR |
208 | } |
209 | ||
3d9d1d66 JB |
210 | return true; |
211 | } | |
212 | ||
9f5e8f6e JB |
213 | bool cfg80211_chandef_usable(struct wiphy *wiphy, |
214 | const struct cfg80211_chan_def *chandef, | |
215 | u32 prohibited_flags) | |
3d9d1d66 | 216 | { |
9f5e8f6e JB |
217 | struct ieee80211_sta_ht_cap *ht_cap; |
218 | struct ieee80211_sta_vht_cap *vht_cap; | |
219 | u32 width, control_freq; | |
3d9d1d66 | 220 | |
9f5e8f6e JB |
221 | if (WARN_ON(!cfg80211_chandef_valid(chandef))) |
222 | return false; | |
3d9d1d66 | 223 | |
9f5e8f6e JB |
224 | ht_cap = &wiphy->bands[chandef->chan->band]->ht_cap; |
225 | vht_cap = &wiphy->bands[chandef->chan->band]->vht_cap; | |
3d9d1d66 | 226 | |
9f5e8f6e | 227 | control_freq = chandef->chan->center_freq; |
9236d838 | 228 | |
3d9d1d66 | 229 | switch (chandef->width) { |
3d9d1d66 | 230 | case NL80211_CHAN_WIDTH_20: |
9f5e8f6e JB |
231 | if (!ht_cap->ht_supported) |
232 | return false; | |
233 | case NL80211_CHAN_WIDTH_20_NOHT: | |
3d9d1d66 JB |
234 | width = 20; |
235 | break; | |
236 | case NL80211_CHAN_WIDTH_40: | |
237 | width = 40; | |
9f5e8f6e JB |
238 | 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; | |
3d9d1d66 | 249 | break; |
3d9d1d66 | 250 | case NL80211_CHAN_WIDTH_80P80: |
9f5e8f6e JB |
251 | 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; | |
3d9d1d66 JB |
256 | width = 80; |
257 | break; | |
258 | case NL80211_CHAN_WIDTH_160: | |
9f5e8f6e JB |
259 | if (!vht_cap->vht_supported) |
260 | return false; | |
261 | if (!(vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ)) | |
262 | return false; | |
3d9d1d66 JB |
263 | width = 160; |
264 | break; | |
265 | default: | |
266 | WARN_ON_ONCE(1); | |
9236d838 | 267 | return false; |
4ee3e063 | 268 | } |
3d9d1d66 | 269 | |
9f5e8f6e JB |
270 | /* TODO: missing regulatory check on 80/160 bandwidth */ |
271 | ||
a6662dba JB |
272 | if (width > 20) |
273 | prohibited_flags |= IEEE80211_CHAN_NO_OFDM; | |
274 | ||
9f5e8f6e JB |
275 | 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 | } | |
284 | EXPORT_SYMBOL(cfg80211_chandef_usable); | |
285 | ||
286 | bool 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); | |
3d9d1d66 | 292 | |
9f5e8f6e JB |
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); | |
3d9d1d66 JB |
298 | |
299 | trace_cfg80211_return_bool(res); | |
300 | return res; | |
9236d838 | 301 | } |
683b6d3b | 302 | EXPORT_SYMBOL(cfg80211_reg_can_beacon); |
9236d838 | 303 | |
e8c9bd5b | 304 | int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev, |
683b6d3b | 305 | struct cfg80211_chan_def *chandef) |
9588bbd5 | 306 | { |
e8c9bd5b | 307 | if (!rdev->ops->set_monitor_channel) |
9588bbd5 | 308 | return -EOPNOTSUPP; |
4f03c1ed MK |
309 | if (!cfg80211_has_monitors_only(rdev)) |
310 | return -EBUSY; | |
9588bbd5 | 311 | |
683b6d3b | 312 | return rdev_set_monitor_channel(rdev, chandef); |
59bbb6f7 | 313 | } |
26ab9a0c MK |
314 | |
315 | void | |
8e95ea49 | 316 | cfg80211_get_chan_state(struct wireless_dev *wdev, |
26ab9a0c MK |
317 | struct ieee80211_channel **chan, |
318 | enum cfg80211_chan_mode *chanmode) | |
319 | { | |
320 | *chan = NULL; | |
321 | *chanmode = CHAN_MODE_UNDEFINED; | |
322 | ||
26ab9a0c MK |
323 | ASSERT_WDEV_LOCK(wdev); |
324 | ||
98104fde | 325 | if (wdev->netdev && !netif_running(wdev->netdev)) |
26ab9a0c MK |
326 | 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: | |
f53594a0 FF |
347 | if (wdev->beacon_interval) { |
348 | *chan = wdev->channel; | |
349 | *chanmode = CHAN_MODE_SHARED; | |
350 | } | |
351 | return; | |
26ab9a0c | 352 | case NL80211_IFTYPE_MESH_POINT: |
f53594a0 FF |
353 | if (wdev->mesh_id_len) { |
354 | *chan = wdev->channel; | |
355 | *chanmode = CHAN_MODE_SHARED; | |
356 | } | |
26ab9a0c MK |
357 | 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; | |
98104fde JB |
363 | case NL80211_IFTYPE_P2P_DEVICE: |
364 | if (wdev->wiphy->features & | |
365 | NL80211_FEATURE_P2P_DEVICE_NEEDS_CHANNEL) | |
366 | *chanmode = CHAN_MODE_EXCLUSIVE; | |
367 | return; | |
26ab9a0c MK |
368 | case NL80211_IFTYPE_UNSPECIFIED: |
369 | case NUM_NL80211_IFTYPES: | |
370 | WARN_ON(1); | |
371 | } | |
372 | ||
373 | return; | |
374 | } |