2 * This file contains helper code to handle channel
3 * settings and keeping track of what is possible at
6 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
9 #include <linux/export.h>
10 #include <net/cfg80211.h>
14 void cfg80211_chandef_create(struct cfg80211_chan_def
*chandef
,
15 struct ieee80211_channel
*chan
,
16 enum nl80211_channel_type chan_type
)
22 chandef
->center_freq2
= 0;
25 case NL80211_CHAN_NO_HT
:
26 chandef
->width
= NL80211_CHAN_WIDTH_20_NOHT
;
27 chandef
->center_freq1
= chan
->center_freq
;
29 case NL80211_CHAN_HT20
:
30 chandef
->width
= NL80211_CHAN_WIDTH_20
;
31 chandef
->center_freq1
= chan
->center_freq
;
33 case NL80211_CHAN_HT40PLUS
:
34 chandef
->width
= NL80211_CHAN_WIDTH_40
;
35 chandef
->center_freq1
= chan
->center_freq
+ 10;
37 case NL80211_CHAN_HT40MINUS
:
38 chandef
->width
= NL80211_CHAN_WIDTH_40
;
39 chandef
->center_freq1
= chan
->center_freq
- 10;
45 EXPORT_SYMBOL(cfg80211_chandef_create
);
47 bool cfg80211_chandef_valid(const struct cfg80211_chan_def
*chandef
)
54 control_freq
= chandef
->chan
->center_freq
;
56 switch (chandef
->width
) {
57 case NL80211_CHAN_WIDTH_5
:
58 case NL80211_CHAN_WIDTH_10
:
59 case NL80211_CHAN_WIDTH_20
:
60 case NL80211_CHAN_WIDTH_20_NOHT
:
61 if (chandef
->center_freq1
!= control_freq
)
63 if (chandef
->center_freq2
)
66 case NL80211_CHAN_WIDTH_40
:
67 if (chandef
->center_freq1
!= control_freq
+ 10 &&
68 chandef
->center_freq1
!= control_freq
- 10)
70 if (chandef
->center_freq2
)
73 case NL80211_CHAN_WIDTH_80P80
:
74 if (chandef
->center_freq1
!= control_freq
+ 30 &&
75 chandef
->center_freq1
!= control_freq
+ 10 &&
76 chandef
->center_freq1
!= control_freq
- 10 &&
77 chandef
->center_freq1
!= control_freq
- 30)
79 if (!chandef
->center_freq2
)
81 /* adjacent is not allowed -- that's a 160 MHz channel */
82 if (chandef
->center_freq1
- chandef
->center_freq2
== 80 ||
83 chandef
->center_freq2
- chandef
->center_freq1
== 80)
86 case NL80211_CHAN_WIDTH_80
:
87 if (chandef
->center_freq1
!= control_freq
+ 30 &&
88 chandef
->center_freq1
!= control_freq
+ 10 &&
89 chandef
->center_freq1
!= control_freq
- 10 &&
90 chandef
->center_freq1
!= control_freq
- 30)
92 if (chandef
->center_freq2
)
95 case NL80211_CHAN_WIDTH_160
:
96 if (chandef
->center_freq1
!= control_freq
+ 70 &&
97 chandef
->center_freq1
!= control_freq
+ 50 &&
98 chandef
->center_freq1
!= control_freq
+ 30 &&
99 chandef
->center_freq1
!= control_freq
+ 10 &&
100 chandef
->center_freq1
!= control_freq
- 10 &&
101 chandef
->center_freq1
!= control_freq
- 30 &&
102 chandef
->center_freq1
!= control_freq
- 50 &&
103 chandef
->center_freq1
!= control_freq
- 70)
105 if (chandef
->center_freq2
)
114 EXPORT_SYMBOL(cfg80211_chandef_valid
);
116 static void chandef_primary_freqs(const struct cfg80211_chan_def
*c
,
117 int *pri40
, int *pri80
)
122 case NL80211_CHAN_WIDTH_40
:
123 *pri40
= c
->center_freq1
;
126 case NL80211_CHAN_WIDTH_80
:
127 case NL80211_CHAN_WIDTH_80P80
:
128 *pri80
= c
->center_freq1
;
130 tmp
= (30 + c
->chan
->center_freq
- c
->center_freq1
)/20;
134 *pri40
= c
->center_freq1
- 20 + 40 * tmp
;
136 case NL80211_CHAN_WIDTH_160
:
138 tmp
= (70 + c
->chan
->center_freq
- c
->center_freq1
)/20;
142 *pri40
= c
->center_freq1
- 60 + 40 * tmp
;
145 *pri80
= c
->center_freq1
- 40 + 80 * tmp
;
152 static int cfg80211_chandef_get_width(const struct cfg80211_chan_def
*c
)
157 case NL80211_CHAN_WIDTH_5
:
160 case NL80211_CHAN_WIDTH_10
:
163 case NL80211_CHAN_WIDTH_20
:
164 case NL80211_CHAN_WIDTH_20_NOHT
:
167 case NL80211_CHAN_WIDTH_40
:
170 case NL80211_CHAN_WIDTH_80P80
:
171 case NL80211_CHAN_WIDTH_80
:
174 case NL80211_CHAN_WIDTH_160
:
184 const struct cfg80211_chan_def
*
185 cfg80211_chandef_compatible(const struct cfg80211_chan_def
*c1
,
186 const struct cfg80211_chan_def
*c2
)
188 u32 c1_pri40
, c1_pri80
, c2_pri40
, c2_pri80
;
190 /* If they are identical, return */
191 if (cfg80211_chandef_identical(c1
, c2
))
194 /* otherwise, must have same control channel */
195 if (c1
->chan
!= c2
->chan
)
199 * If they have the same width, but aren't identical,
200 * then they can't be compatible.
202 if (c1
->width
== c2
->width
)
206 * can't be compatible if one of them is 5 or 10 MHz,
207 * but they don't have the same width.
209 if (c1
->width
== NL80211_CHAN_WIDTH_5
||
210 c1
->width
== NL80211_CHAN_WIDTH_10
||
211 c2
->width
== NL80211_CHAN_WIDTH_5
||
212 c2
->width
== NL80211_CHAN_WIDTH_10
)
215 if (c1
->width
== NL80211_CHAN_WIDTH_20_NOHT
||
216 c1
->width
== NL80211_CHAN_WIDTH_20
)
219 if (c2
->width
== NL80211_CHAN_WIDTH_20_NOHT
||
220 c2
->width
== NL80211_CHAN_WIDTH_20
)
223 chandef_primary_freqs(c1
, &c1_pri40
, &c1_pri80
);
224 chandef_primary_freqs(c2
, &c2_pri40
, &c2_pri80
);
226 if (c1_pri40
!= c2_pri40
)
229 WARN_ON(!c1_pri80
&& !c2_pri80
);
230 if (c1_pri80
&& c2_pri80
&& c1_pri80
!= c2_pri80
)
233 if (c1
->width
> c2
->width
)
237 EXPORT_SYMBOL(cfg80211_chandef_compatible
);
239 static void cfg80211_set_chans_dfs_state(struct wiphy
*wiphy
, u32 center_freq
,
241 enum nl80211_dfs_state dfs_state
)
243 struct ieee80211_channel
*c
;
246 for (freq
= center_freq
- bandwidth
/2 + 10;
247 freq
<= center_freq
+ bandwidth
/2 - 10;
249 c
= ieee80211_get_channel(wiphy
, freq
);
250 if (!c
|| !(c
->flags
& IEEE80211_CHAN_RADAR
))
253 c
->dfs_state
= dfs_state
;
254 c
->dfs_state_entered
= jiffies
;
258 void cfg80211_set_dfs_state(struct wiphy
*wiphy
,
259 const struct cfg80211_chan_def
*chandef
,
260 enum nl80211_dfs_state dfs_state
)
264 if (WARN_ON(!cfg80211_chandef_valid(chandef
)))
267 width
= cfg80211_chandef_get_width(chandef
);
271 cfg80211_set_chans_dfs_state(wiphy
, chandef
->center_freq1
,
274 if (!chandef
->center_freq2
)
276 cfg80211_set_chans_dfs_state(wiphy
, chandef
->center_freq2
,
280 static int cfg80211_get_chans_dfs_required(struct wiphy
*wiphy
,
284 struct ieee80211_channel
*c
;
285 u32 freq
, start_freq
, end_freq
;
287 if (bandwidth
<= 20) {
288 start_freq
= center_freq
;
289 end_freq
= center_freq
;
291 start_freq
= center_freq
- bandwidth
/2 + 10;
292 end_freq
= center_freq
+ bandwidth
/2 - 10;
295 for (freq
= start_freq
; freq
<= end_freq
; freq
+= 20) {
296 c
= ieee80211_get_channel(wiphy
, freq
);
300 if (c
->flags
& IEEE80211_CHAN_RADAR
)
307 int cfg80211_chandef_dfs_required(struct wiphy
*wiphy
,
308 const struct cfg80211_chan_def
*chandef
)
313 if (WARN_ON(!cfg80211_chandef_valid(chandef
)))
316 width
= cfg80211_chandef_get_width(chandef
);
320 r
= cfg80211_get_chans_dfs_required(wiphy
, chandef
->center_freq1
,
325 if (!chandef
->center_freq2
)
328 return cfg80211_get_chans_dfs_required(wiphy
, chandef
->center_freq2
,
332 static bool cfg80211_secondary_chans_ok(struct wiphy
*wiphy
,
333 u32 center_freq
, u32 bandwidth
,
334 u32 prohibited_flags
)
336 struct ieee80211_channel
*c
;
337 u32 freq
, start_freq
, end_freq
;
339 if (bandwidth
<= 20) {
340 start_freq
= center_freq
;
341 end_freq
= center_freq
;
343 start_freq
= center_freq
- bandwidth
/2 + 10;
344 end_freq
= center_freq
+ bandwidth
/2 - 10;
347 for (freq
= start_freq
; freq
<= end_freq
; freq
+= 20) {
348 c
= ieee80211_get_channel(wiphy
, freq
);
352 /* check for radar flags */
353 if ((prohibited_flags
& c
->flags
& IEEE80211_CHAN_RADAR
) &&
354 (c
->dfs_state
!= NL80211_DFS_AVAILABLE
))
357 /* check for the other flags */
358 if (c
->flags
& prohibited_flags
& ~IEEE80211_CHAN_RADAR
)
365 bool cfg80211_chandef_usable(struct wiphy
*wiphy
,
366 const struct cfg80211_chan_def
*chandef
,
367 u32 prohibited_flags
)
369 struct ieee80211_sta_ht_cap
*ht_cap
;
370 struct ieee80211_sta_vht_cap
*vht_cap
;
371 u32 width
, control_freq
;
373 if (WARN_ON(!cfg80211_chandef_valid(chandef
)))
376 ht_cap
= &wiphy
->bands
[chandef
->chan
->band
]->ht_cap
;
377 vht_cap
= &wiphy
->bands
[chandef
->chan
->band
]->vht_cap
;
379 control_freq
= chandef
->chan
->center_freq
;
381 switch (chandef
->width
) {
382 case NL80211_CHAN_WIDTH_5
:
385 case NL80211_CHAN_WIDTH_10
:
388 case NL80211_CHAN_WIDTH_20
:
389 if (!ht_cap
->ht_supported
)
391 case NL80211_CHAN_WIDTH_20_NOHT
:
394 case NL80211_CHAN_WIDTH_40
:
396 if (!ht_cap
->ht_supported
)
398 if (!(ht_cap
->cap
& IEEE80211_HT_CAP_SUP_WIDTH_20_40
) ||
399 ht_cap
->cap
& IEEE80211_HT_CAP_40MHZ_INTOLERANT
)
401 if (chandef
->center_freq1
< control_freq
&&
402 chandef
->chan
->flags
& IEEE80211_CHAN_NO_HT40MINUS
)
404 if (chandef
->center_freq1
> control_freq
&&
405 chandef
->chan
->flags
& IEEE80211_CHAN_NO_HT40PLUS
)
408 case NL80211_CHAN_WIDTH_80P80
:
409 if (!(vht_cap
->cap
& IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ
))
411 case NL80211_CHAN_WIDTH_80
:
412 if (!vht_cap
->vht_supported
)
414 prohibited_flags
|= IEEE80211_CHAN_NO_80MHZ
;
417 case NL80211_CHAN_WIDTH_160
:
418 if (!vht_cap
->vht_supported
)
420 if (!(vht_cap
->cap
& IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ
))
422 prohibited_flags
|= IEEE80211_CHAN_NO_160MHZ
;
431 * TODO: What if there are only certain 80/160/80+80 MHz channels
432 * allowed by the driver, or only certain combinations?
433 * For 40 MHz the driver can set the NO_HT40 flags, but for
434 * 80/160 MHz and in particular 80+80 MHz this isn't really
435 * feasible and we only have NO_80MHZ/NO_160MHZ so far but
436 * no way to cover 80+80 MHz or more complex restrictions.
437 * Note that such restrictions also need to be advertised to
438 * userspace, for example for P2P channel selection.
442 prohibited_flags
|= IEEE80211_CHAN_NO_OFDM
;
444 /* 5 and 10 MHz are only defined for the OFDM PHY */
446 prohibited_flags
|= IEEE80211_CHAN_NO_OFDM
;
449 if (!cfg80211_secondary_chans_ok(wiphy
, chandef
->center_freq1
,
450 width
, prohibited_flags
))
453 if (!chandef
->center_freq2
)
455 return cfg80211_secondary_chans_ok(wiphy
, chandef
->center_freq2
,
456 width
, prohibited_flags
);
458 EXPORT_SYMBOL(cfg80211_chandef_usable
);
460 bool cfg80211_reg_can_beacon(struct wiphy
*wiphy
,
461 struct cfg80211_chan_def
*chandef
)
465 trace_cfg80211_reg_can_beacon(wiphy
, chandef
);
467 res
= cfg80211_chandef_usable(wiphy
, chandef
,
468 IEEE80211_CHAN_DISABLED
|
469 IEEE80211_CHAN_PASSIVE_SCAN
|
470 IEEE80211_CHAN_NO_IBSS
|
471 IEEE80211_CHAN_RADAR
);
473 trace_cfg80211_return_bool(res
);
476 EXPORT_SYMBOL(cfg80211_reg_can_beacon
);
478 int cfg80211_set_monitor_channel(struct cfg80211_registered_device
*rdev
,
479 struct cfg80211_chan_def
*chandef
)
481 if (!rdev
->ops
->set_monitor_channel
)
483 if (!cfg80211_has_monitors_only(rdev
))
486 return rdev_set_monitor_channel(rdev
, chandef
);
490 cfg80211_get_chan_state(struct wireless_dev
*wdev
,
491 struct ieee80211_channel
**chan
,
492 enum cfg80211_chan_mode
*chanmode
)
495 *chanmode
= CHAN_MODE_UNDEFINED
;
497 ASSERT_WDEV_LOCK(wdev
);
499 if (wdev
->netdev
&& !netif_running(wdev
->netdev
))
502 switch (wdev
->iftype
) {
503 case NL80211_IFTYPE_ADHOC
:
504 if (wdev
->current_bss
) {
505 *chan
= wdev
->current_bss
->pub
.channel
;
506 *chanmode
= wdev
->ibss_fixed
508 : CHAN_MODE_EXCLUSIVE
;
511 case NL80211_IFTYPE_STATION
:
512 case NL80211_IFTYPE_P2P_CLIENT
:
513 if (wdev
->current_bss
) {
514 *chan
= wdev
->current_bss
->pub
.channel
;
515 *chanmode
= CHAN_MODE_SHARED
;
519 case NL80211_IFTYPE_AP
:
520 case NL80211_IFTYPE_P2P_GO
:
521 if (wdev
->cac_started
) {
522 *chan
= wdev
->channel
;
523 *chanmode
= CHAN_MODE_SHARED
;
524 } else if (wdev
->beacon_interval
) {
525 *chan
= wdev
->channel
;
526 *chanmode
= CHAN_MODE_SHARED
;
529 case NL80211_IFTYPE_MESH_POINT
:
530 if (wdev
->mesh_id_len
) {
531 *chan
= wdev
->channel
;
532 *chanmode
= CHAN_MODE_SHARED
;
535 case NL80211_IFTYPE_MONITOR
:
536 case NL80211_IFTYPE_AP_VLAN
:
537 case NL80211_IFTYPE_WDS
:
538 /* these interface types don't really have a channel */
540 case NL80211_IFTYPE_P2P_DEVICE
:
541 if (wdev
->wiphy
->features
&
542 NL80211_FEATURE_P2P_DEVICE_NEEDS_CHANNEL
)
543 *chanmode
= CHAN_MODE_EXCLUSIVE
;
545 case NL80211_IFTYPE_UNSPECIFIED
:
546 case NUM_NL80211_IFTYPES
: