2 * DFS - Dynamic Frequency Selection
3 * Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi>
4 * Copyright (c) 2013-2017, Qualcomm Atheros, Inc.
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
10 #include "utils/includes.h"
12 #include "utils/common.h"
13 #include "common/ieee802_11_defs.h"
14 #include "common/hw_features_common.h"
15 #include "common/wpa_ctrl.h"
17 #include "ap_drv_ops.h"
18 #include "drivers/driver.h"
22 static int dfs_get_used_n_chans(struct hostapd_iface
*iface
, int *seg1
)
28 if (iface
->conf
->ieee80211n
&& iface
->conf
->secondary_channel
)
31 if (iface
->conf
->ieee80211ac
) {
32 switch (iface
->conf
->vht_oper_chwidth
) {
33 case VHT_CHANWIDTH_USE_HT
:
35 case VHT_CHANWIDTH_80MHZ
:
38 case VHT_CHANWIDTH_160MHZ
:
41 case VHT_CHANWIDTH_80P80MHZ
:
54 static int dfs_channel_available(struct hostapd_channel_data
*chan
,
58 * When radar detection happens, CSA is performed. However, there's no
59 * time for CAC, so radar channels must be skipped when finding a new
60 * channel for CSA, unless they are available for immediate use.
62 if (skip_radar
&& (chan
->flag
& HOSTAPD_CHAN_RADAR
) &&
63 ((chan
->flag
& HOSTAPD_CHAN_DFS_MASK
) !=
64 HOSTAPD_CHAN_DFS_AVAILABLE
))
67 if (chan
->flag
& HOSTAPD_CHAN_DISABLED
)
69 if ((chan
->flag
& HOSTAPD_CHAN_RADAR
) &&
70 ((chan
->flag
& HOSTAPD_CHAN_DFS_MASK
) ==
71 HOSTAPD_CHAN_DFS_UNAVAILABLE
))
77 static int dfs_is_chan_allowed(struct hostapd_channel_data
*chan
, int n_chans
)
80 * The tables contain first valid channel number based on channel width.
81 * We will also choose this first channel as the control one.
83 int allowed_40
[] = { 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157,
86 * VHT80, valid channels based on center frequency:
87 * 42, 58, 106, 122, 138, 155
89 int allowed_80
[] = { 36, 52, 100, 116, 132, 149 };
91 * VHT160 valid channels based on center frequency:
94 int allowed_160
[] = { 36, 100 };
95 int *allowed
= allowed_40
;
96 unsigned int i
, allowed_no
= 0;
100 allowed
= allowed_40
;
101 allowed_no
= ARRAY_SIZE(allowed_40
);
104 allowed
= allowed_80
;
105 allowed_no
= ARRAY_SIZE(allowed_80
);
108 allowed
= allowed_160
;
109 allowed_no
= ARRAY_SIZE(allowed_160
);
112 wpa_printf(MSG_DEBUG
, "Unknown width for %d channels", n_chans
);
116 for (i
= 0; i
< allowed_no
; i
++) {
117 if (chan
->chan
== allowed
[i
])
125 static struct hostapd_channel_data
*
126 dfs_get_chan_data(struct hostapd_hw_modes
*mode
, int freq
, int first_chan_idx
)
130 for (i
= first_chan_idx
; i
< mode
->num_channels
; i
++) {
131 if (mode
->channels
[i
].freq
== freq
)
132 return &mode
->channels
[i
];
139 static int dfs_chan_range_available(struct hostapd_hw_modes
*mode
,
140 int first_chan_idx
, int num_chans
,
143 struct hostapd_channel_data
*first_chan
, *chan
;
146 if (first_chan_idx
+ num_chans
> mode
->num_channels
)
149 first_chan
= &mode
->channels
[first_chan_idx
];
151 for (i
= 0; i
< num_chans
; i
++) {
152 chan
= dfs_get_chan_data(mode
, first_chan
->freq
+ i
* 20,
157 if (!dfs_channel_available(chan
, skip_radar
))
165 static int is_in_chanlist(struct hostapd_iface
*iface
,
166 struct hostapd_channel_data
*chan
)
168 if (!iface
->conf
->acs_ch_list
.num
)
171 return freq_range_list_includes(&iface
->conf
->acs_ch_list
, chan
->chan
);
176 * The function assumes HT40+ operation.
177 * Make sure to adjust the following variables after calling this:
178 * - hapd->secondary_channel
179 * - hapd->vht_oper_centr_freq_seg0_idx
180 * - hapd->vht_oper_centr_freq_seg1_idx
182 static int dfs_find_channel(struct hostapd_iface
*iface
,
183 struct hostapd_channel_data
**ret_chan
,
184 int idx
, int skip_radar
)
186 struct hostapd_hw_modes
*mode
;
187 struct hostapd_channel_data
*chan
;
188 int i
, channel_idx
= 0, n_chans
, n_chans1
;
190 mode
= iface
->current_mode
;
191 n_chans
= dfs_get_used_n_chans(iface
, &n_chans1
);
193 wpa_printf(MSG_DEBUG
, "DFS new chan checking %d channels", n_chans
);
194 for (i
= 0; i
< mode
->num_channels
; i
++) {
195 chan
= &mode
->channels
[i
];
197 /* Skip HT40/VHT incompatible channels */
198 if (iface
->conf
->ieee80211n
&&
199 iface
->conf
->secondary_channel
&&
200 !dfs_is_chan_allowed(chan
, n_chans
))
203 /* Skip incompatible chandefs */
204 if (!dfs_chan_range_available(mode
, i
, n_chans
, skip_radar
))
207 if (!is_in_chanlist(iface
, chan
))
210 if (ret_chan
&& idx
== channel_idx
) {
211 wpa_printf(MSG_DEBUG
, "Selected ch. #%d", chan
->chan
);
215 wpa_printf(MSG_DEBUG
, "Adding channel: %d", chan
->chan
);
222 static void dfs_adjust_vht_center_freq(struct hostapd_iface
*iface
,
223 struct hostapd_channel_data
*chan
,
224 int secondary_channel
,
225 u8
*vht_oper_centr_freq_seg0_idx
,
226 u8
*vht_oper_centr_freq_seg1_idx
)
228 if (!iface
->conf
->ieee80211ac
)
234 *vht_oper_centr_freq_seg1_idx
= 0;
236 switch (iface
->conf
->vht_oper_chwidth
) {
237 case VHT_CHANWIDTH_USE_HT
:
238 if (secondary_channel
== 1)
239 *vht_oper_centr_freq_seg0_idx
= chan
->chan
+ 2;
240 else if (secondary_channel
== -1)
241 *vht_oper_centr_freq_seg0_idx
= chan
->chan
- 2;
243 *vht_oper_centr_freq_seg0_idx
= chan
->chan
;
245 case VHT_CHANWIDTH_80MHZ
:
246 *vht_oper_centr_freq_seg0_idx
= chan
->chan
+ 6;
248 case VHT_CHANWIDTH_160MHZ
:
249 *vht_oper_centr_freq_seg0_idx
= chan
->chan
+ 14;
252 wpa_printf(MSG_INFO
, "DFS only VHT20/40/80/160 is supported now");
253 *vht_oper_centr_freq_seg0_idx
= 0;
257 wpa_printf(MSG_DEBUG
, "DFS adjusting VHT center frequency: %d, %d",
258 *vht_oper_centr_freq_seg0_idx
,
259 *vht_oper_centr_freq_seg1_idx
);
263 /* Return start channel idx we will use for mode->channels[idx] */
264 static int dfs_get_start_chan_idx(struct hostapd_iface
*iface
, int *seg1_start
)
266 struct hostapd_hw_modes
*mode
;
267 struct hostapd_channel_data
*chan
;
268 int channel_no
= iface
->conf
->channel
;
275 if (iface
->conf
->ieee80211n
&& iface
->conf
->secondary_channel
== -1)
279 if (iface
->conf
->ieee80211ac
) {
280 switch (iface
->conf
->vht_oper_chwidth
) {
281 case VHT_CHANWIDTH_USE_HT
:
283 case VHT_CHANWIDTH_80MHZ
:
285 iface
->conf
->vht_oper_centr_freq_seg0_idx
- 6;
287 case VHT_CHANWIDTH_160MHZ
:
289 iface
->conf
->vht_oper_centr_freq_seg0_idx
- 14;
291 case VHT_CHANWIDTH_80P80MHZ
:
293 iface
->conf
->vht_oper_centr_freq_seg0_idx
- 6;
295 iface
->conf
->vht_oper_centr_freq_seg1_idx
- 6;
299 "DFS only VHT20/40/80/160/80+80 is supported now");
306 mode
= iface
->current_mode
;
307 for (i
= 0; i
< mode
->num_channels
; i
++) {
308 chan
= &mode
->channels
[i
];
309 if (chan
->chan
== channel_no
) {
315 if (res
!= -1 && chan_seg1
> -1) {
318 /* Get idx for seg1 */
319 mode
= iface
->current_mode
;
320 for (i
= 0; i
< mode
->num_channels
; i
++) {
321 chan
= &mode
->channels
[i
];
322 if (chan
->chan
== chan_seg1
) {
333 wpa_printf(MSG_DEBUG
,
334 "DFS chan_idx seems wrong; num-ch: %d ch-no: %d conf-ch-no: %d 11n: %d sec-ch: %d vht-oper-width: %d",
335 mode
->num_channels
, channel_no
, iface
->conf
->channel
,
336 iface
->conf
->ieee80211n
,
337 iface
->conf
->secondary_channel
,
338 iface
->conf
->vht_oper_chwidth
);
340 for (i
= 0; i
< mode
->num_channels
; i
++) {
341 wpa_printf(MSG_DEBUG
, "Available channel: %d",
342 mode
->channels
[i
].chan
);
350 /* At least one channel have radar flag */
351 static int dfs_check_chans_radar(struct hostapd_iface
*iface
,
352 int start_chan_idx
, int n_chans
)
354 struct hostapd_channel_data
*channel
;
355 struct hostapd_hw_modes
*mode
;
358 mode
= iface
->current_mode
;
360 for (i
= 0; i
< n_chans
; i
++) {
361 channel
= &mode
->channels
[start_chan_idx
+ i
];
362 if (channel
->flag
& HOSTAPD_CHAN_RADAR
)
370 /* All channels available */
371 static int dfs_check_chans_available(struct hostapd_iface
*iface
,
372 int start_chan_idx
, int n_chans
)
374 struct hostapd_channel_data
*channel
;
375 struct hostapd_hw_modes
*mode
;
378 mode
= iface
->current_mode
;
380 for (i
= 0; i
< n_chans
; i
++) {
381 channel
= &mode
->channels
[start_chan_idx
+ i
];
383 if (channel
->flag
& HOSTAPD_CHAN_DISABLED
)
386 if (!(channel
->flag
& HOSTAPD_CHAN_RADAR
))
389 if ((channel
->flag
& HOSTAPD_CHAN_DFS_MASK
) !=
390 HOSTAPD_CHAN_DFS_AVAILABLE
)
398 /* At least one channel unavailable */
399 static int dfs_check_chans_unavailable(struct hostapd_iface
*iface
,
403 struct hostapd_channel_data
*channel
;
404 struct hostapd_hw_modes
*mode
;
407 mode
= iface
->current_mode
;
409 for (i
= 0; i
< n_chans
; i
++) {
410 channel
= &mode
->channels
[start_chan_idx
+ i
];
411 if (channel
->flag
& HOSTAPD_CHAN_DISABLED
)
413 if ((channel
->flag
& HOSTAPD_CHAN_DFS_MASK
) ==
414 HOSTAPD_CHAN_DFS_UNAVAILABLE
)
422 static struct hostapd_channel_data
*
423 dfs_get_valid_channel(struct hostapd_iface
*iface
,
424 int *secondary_channel
,
425 u8
*vht_oper_centr_freq_seg0_idx
,
426 u8
*vht_oper_centr_freq_seg1_idx
,
429 struct hostapd_hw_modes
*mode
;
430 struct hostapd_channel_data
*chan
= NULL
;
431 int num_available_chandefs
;
435 wpa_printf(MSG_DEBUG
, "DFS: Selecting random channel");
436 *secondary_channel
= 0;
437 *vht_oper_centr_freq_seg0_idx
= 0;
438 *vht_oper_centr_freq_seg1_idx
= 0;
440 if (iface
->current_mode
== NULL
)
443 mode
= iface
->current_mode
;
444 if (mode
->mode
!= HOSTAPD_MODE_IEEE80211A
)
447 /* Get the count first */
448 num_available_chandefs
= dfs_find_channel(iface
, NULL
, 0, skip_radar
);
449 if (num_available_chandefs
== 0)
452 if (os_get_random((u8
*) &_rand
, sizeof(_rand
)) < 0)
454 chan_idx
= _rand
% num_available_chandefs
;
455 dfs_find_channel(iface
, &chan
, chan_idx
, skip_radar
);
457 /* dfs_find_channel() calculations assume HT40+ */
458 if (iface
->conf
->secondary_channel
)
459 *secondary_channel
= 1;
461 *secondary_channel
= 0;
463 dfs_adjust_vht_center_freq(iface
, chan
,
465 vht_oper_centr_freq_seg0_idx
,
466 vht_oper_centr_freq_seg1_idx
);
472 static int set_dfs_state_freq(struct hostapd_iface
*iface
, int freq
, u32 state
)
474 struct hostapd_hw_modes
*mode
;
475 struct hostapd_channel_data
*chan
= NULL
;
478 mode
= iface
->current_mode
;
482 wpa_printf(MSG_DEBUG
, "set_dfs_state 0x%X for %d MHz", state
, freq
);
483 for (i
= 0; i
< iface
->current_mode
->num_channels
; i
++) {
484 chan
= &iface
->current_mode
->channels
[i
];
485 if (chan
->freq
== freq
) {
486 if (chan
->flag
& HOSTAPD_CHAN_RADAR
) {
487 chan
->flag
&= ~HOSTAPD_CHAN_DFS_MASK
;
489 return 1; /* Channel found */
493 wpa_printf(MSG_WARNING
, "Can't set DFS state for freq %d MHz", freq
);
498 static int set_dfs_state(struct hostapd_iface
*iface
, int freq
, int ht_enabled
,
499 int chan_offset
, int chan_width
, int cf1
,
503 struct hostapd_hw_modes
*mode
;
504 int frequency
= freq
;
507 mode
= iface
->current_mode
;
511 if (mode
->mode
!= HOSTAPD_MODE_IEEE80211A
) {
512 wpa_printf(MSG_WARNING
, "current_mode != IEEE80211A");
516 /* Seems cf1 and chan_width is enough here */
517 switch (chan_width
) {
518 case CHAN_WIDTH_20_NOHT
:
526 frequency
= cf1
- 10;
530 frequency
= cf1
- 30;
534 frequency
= cf1
- 70;
537 wpa_printf(MSG_INFO
, "DFS chan_width %d not supported",
542 wpa_printf(MSG_DEBUG
, "DFS freq: %dMHz, n_chans: %d", frequency
,
544 for (i
= 0; i
< n_chans
; i
++) {
545 ret
+= set_dfs_state_freq(iface
, frequency
, state
);
546 frequency
= frequency
+ 20;
553 static int dfs_are_channels_overlapped(struct hostapd_iface
*iface
, int freq
,
554 int chan_width
, int cf1
, int cf2
)
556 int start_chan_idx
, start_chan_idx1
;
557 struct hostapd_hw_modes
*mode
;
558 struct hostapd_channel_data
*chan
;
559 int n_chans
, n_chans1
, i
, j
, frequency
= freq
, radar_n_chans
= 1;
563 /* Our configuration */
564 mode
= iface
->current_mode
;
565 start_chan_idx
= dfs_get_start_chan_idx(iface
, &start_chan_idx1
);
566 n_chans
= dfs_get_used_n_chans(iface
, &n_chans1
);
568 /* Check we are on DFS channel(s) */
569 if (!dfs_check_chans_radar(iface
, start_chan_idx
, n_chans
))
572 /* Reported via radar event */
573 switch (chan_width
) {
574 case CHAN_WIDTH_20_NOHT
:
582 frequency
= cf1
- 10;
586 frequency
= cf1
- 30;
590 frequency
= cf1
- 70;
593 wpa_printf(MSG_INFO
, "DFS chan_width %d not supported",
598 ieee80211_freq_to_chan(frequency
, &radar_chan
);
600 for (i
= 0; i
< n_chans
; i
++) {
601 chan
= &mode
->channels
[start_chan_idx
+ i
];
602 if (!(chan
->flag
& HOSTAPD_CHAN_RADAR
))
604 for (j
= 0; j
< radar_n_chans
; j
++) {
605 wpa_printf(MSG_DEBUG
, "checking our: %d, radar: %d",
606 chan
->chan
, radar_chan
+ j
* 4);
607 if (chan
->chan
== radar_chan
+ j
* 4)
612 wpa_printf(MSG_DEBUG
, "overlapped: %d", res
);
618 static unsigned int dfs_get_cac_time(struct hostapd_iface
*iface
,
619 int start_chan_idx
, int n_chans
)
621 struct hostapd_channel_data
*channel
;
622 struct hostapd_hw_modes
*mode
;
624 unsigned int cac_time_ms
= 0;
626 mode
= iface
->current_mode
;
628 for (i
= 0; i
< n_chans
; i
++) {
629 channel
= &mode
->channels
[start_chan_idx
+ i
];
630 if (!(channel
->flag
& HOSTAPD_CHAN_RADAR
))
632 if (channel
->dfs_cac_ms
> cac_time_ms
)
633 cac_time_ms
= channel
->dfs_cac_ms
;
642 * 1 - continue channel/ap setup
643 * 0 - channel/ap setup will be continued after CAC
644 * -1 - hit critical error
646 int hostapd_handle_dfs(struct hostapd_iface
*iface
)
648 struct hostapd_channel_data
*channel
;
649 int res
, n_chans
, n_chans1
, start_chan_idx
, start_chan_idx1
;
652 if (!iface
->current_mode
) {
654 * This can happen with drivers that do not provide mode
655 * information and as such, cannot really use hostapd for DFS.
657 wpa_printf(MSG_DEBUG
,
658 "DFS: No current_mode information - assume no need to perform DFS operations by hostapd");
662 iface
->cac_started
= 0;
665 /* Get start (first) channel for current configuration */
666 start_chan_idx
= dfs_get_start_chan_idx(iface
,
668 if (start_chan_idx
== -1)
671 /* Get number of used channels, depend on width */
672 n_chans
= dfs_get_used_n_chans(iface
, &n_chans1
);
675 iface
->dfs_cac_ms
= dfs_get_cac_time(iface
, start_chan_idx
,
678 /* Check if any of configured channels require DFS */
679 res
= dfs_check_chans_radar(iface
, start_chan_idx
, n_chans
);
680 wpa_printf(MSG_DEBUG
,
681 "DFS %d channels required radar detection",
686 /* Check if all channels are DFS available */
687 res
= dfs_check_chans_available(iface
, start_chan_idx
, n_chans
);
688 wpa_printf(MSG_DEBUG
,
689 "DFS all channels available, (SKIP CAC): %s",
694 /* Check if any of configured channels is unavailable */
695 res
= dfs_check_chans_unavailable(iface
, start_chan_idx
,
697 wpa_printf(MSG_DEBUG
, "DFS %d chans unavailable - choose other channel: %s",
698 res
, res
? "yes": "no");
703 channel
= dfs_get_valid_channel(iface
, &sec
, &cf1
, &cf2
,
706 wpa_printf(MSG_ERROR
, "could not get valid channel");
707 hostapd_set_state(iface
, HAPD_IFACE_DFS
);
711 iface
->freq
= channel
->freq
;
712 iface
->conf
->channel
= channel
->chan
;
713 iface
->conf
->secondary_channel
= sec
;
714 iface
->conf
->vht_oper_centr_freq_seg0_idx
= cf1
;
715 iface
->conf
->vht_oper_centr_freq_seg1_idx
= cf2
;
719 /* Finally start CAC */
720 hostapd_set_state(iface
, HAPD_IFACE_DFS
);
721 wpa_printf(MSG_DEBUG
, "DFS start CAC on %d MHz", iface
->freq
);
722 wpa_msg(iface
->bss
[0]->msg_ctx
, MSG_INFO
, DFS_EVENT_CAC_START
723 "freq=%d chan=%d sec_chan=%d, width=%d, seg0=%d, seg1=%d, cac_time=%ds",
725 iface
->conf
->channel
, iface
->conf
->secondary_channel
,
726 iface
->conf
->vht_oper_chwidth
,
727 iface
->conf
->vht_oper_centr_freq_seg0_idx
,
728 iface
->conf
->vht_oper_centr_freq_seg1_idx
,
729 iface
->dfs_cac_ms
/ 1000);
731 res
= hostapd_start_dfs_cac(iface
, iface
->conf
->hw_mode
,
733 iface
->conf
->channel
,
734 iface
->conf
->ieee80211n
,
735 iface
->conf
->ieee80211ac
,
736 iface
->conf
->secondary_channel
,
737 iface
->conf
->vht_oper_chwidth
,
738 iface
->conf
->vht_oper_centr_freq_seg0_idx
,
739 iface
->conf
->vht_oper_centr_freq_seg1_idx
);
742 wpa_printf(MSG_ERROR
, "DFS start_dfs_cac() failed, %d", res
);
750 static int hostapd_config_dfs_chan_available(struct hostapd_iface
*iface
)
752 int n_chans
, n_chans1
, start_chan_idx
, start_chan_idx1
;
754 /* Get the start (first) channel for current configuration */
755 start_chan_idx
= dfs_get_start_chan_idx(iface
, &start_chan_idx1
);
756 if (start_chan_idx
< 0)
759 /* Get the number of used channels, depending on width */
760 n_chans
= dfs_get_used_n_chans(iface
, &n_chans1
);
762 /* Check if all channels are DFS available */
763 return dfs_check_chans_available(iface
, start_chan_idx
, n_chans
);
767 int hostapd_dfs_complete_cac(struct hostapd_iface
*iface
, int success
, int freq
,
768 int ht_enabled
, int chan_offset
, int chan_width
,
771 wpa_msg(iface
->bss
[0]->msg_ctx
, MSG_INFO
, DFS_EVENT_CAC_COMPLETED
772 "success=%d freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
773 success
, freq
, ht_enabled
, chan_offset
, chan_width
, cf1
, cf2
);
776 /* Complete iface/ap configuration */
777 if (iface
->drv_flags
& WPA_DRIVER_FLAGS_DFS_OFFLOAD
) {
778 /* Complete AP configuration for the first bring up. */
779 if (iface
->state
!= HAPD_IFACE_ENABLED
)
780 hostapd_setup_interface_complete(iface
, 0);
782 iface
->cac_started
= 0;
784 set_dfs_state(iface
, freq
, ht_enabled
, chan_offset
,
785 chan_width
, cf1
, cf2
,
786 HOSTAPD_CHAN_DFS_AVAILABLE
);
788 * Just mark the channel available when CAC completion
789 * event is received in enabled state. CAC result could
790 * have been propagated from another radio having the
791 * same regulatory configuration. When CAC completion is
792 * received during non-HAPD_IFACE_ENABLED state, make
793 * sure the configured channel is available because this
794 * CAC completion event could have been propagated from
797 if (iface
->state
!= HAPD_IFACE_ENABLED
&&
798 hostapd_config_dfs_chan_available(iface
)) {
799 hostapd_setup_interface_complete(iface
, 0);
800 iface
->cac_started
= 0;
809 int hostapd_dfs_pre_cac_expired(struct hostapd_iface
*iface
, int freq
,
810 int ht_enabled
, int chan_offset
, int chan_width
,
813 wpa_msg(iface
->bss
[0]->msg_ctx
, MSG_INFO
, DFS_EVENT_PRE_CAC_EXPIRED
814 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
815 freq
, ht_enabled
, chan_offset
, chan_width
, cf1
, cf2
);
817 /* Proceed only if DFS is not offloaded to the driver */
818 if (iface
->drv_flags
& WPA_DRIVER_FLAGS_DFS_OFFLOAD
)
821 set_dfs_state(iface
, freq
, ht_enabled
, chan_offset
, chan_width
,
822 cf1
, cf2
, HOSTAPD_CHAN_DFS_USABLE
);
828 static int hostapd_dfs_start_channel_switch_cac(struct hostapd_iface
*iface
)
830 struct hostapd_channel_data
*channel
;
831 int secondary_channel
;
832 u8 vht_oper_centr_freq_seg0_idx
= 0;
833 u8 vht_oper_centr_freq_seg1_idx
= 0;
837 /* Radar detected during active CAC */
838 iface
->cac_started
= 0;
839 channel
= dfs_get_valid_channel(iface
, &secondary_channel
,
840 &vht_oper_centr_freq_seg0_idx
,
841 &vht_oper_centr_freq_seg1_idx
,
845 wpa_printf(MSG_ERROR
, "No valid channel available");
849 wpa_printf(MSG_DEBUG
, "DFS will switch to a new channel %d",
851 wpa_msg(iface
->bss
[0]->msg_ctx
, MSG_INFO
, DFS_EVENT_NEW_CHANNEL
852 "freq=%d chan=%d sec_chan=%d", channel
->freq
,
853 channel
->chan
, secondary_channel
);
855 iface
->freq
= channel
->freq
;
856 iface
->conf
->channel
= channel
->chan
;
857 iface
->conf
->secondary_channel
= secondary_channel
;
858 iface
->conf
->vht_oper_centr_freq_seg0_idx
=
859 vht_oper_centr_freq_seg0_idx
;
860 iface
->conf
->vht_oper_centr_freq_seg1_idx
=
861 vht_oper_centr_freq_seg1_idx
;
864 hostapd_setup_interface_complete(iface
, err
);
869 static int hostapd_dfs_start_channel_switch(struct hostapd_iface
*iface
)
871 struct hostapd_channel_data
*channel
;
872 int secondary_channel
;
873 u8 vht_oper_centr_freq_seg0_idx
;
874 u8 vht_oper_centr_freq_seg1_idx
;
876 struct csa_settings csa_settings
;
880 wpa_printf(MSG_DEBUG
, "%s called (CAC active: %s, CSA active: %s)",
881 __func__
, iface
->cac_started
? "yes" : "no",
882 hostapd_csa_in_progress(iface
) ? "yes" : "no");
884 /* Check if CSA in progress */
885 if (hostapd_csa_in_progress(iface
))
888 /* Check if active CAC */
889 if (iface
->cac_started
)
890 return hostapd_dfs_start_channel_switch_cac(iface
);
892 /* Perform channel switch/CSA */
893 channel
= dfs_get_valid_channel(iface
, &secondary_channel
,
894 &vht_oper_centr_freq_seg0_idx
,
895 &vht_oper_centr_freq_seg1_idx
,
900 * If there is no channel to switch immediately to, check if
901 * there is another channel where we can switch even if it
902 * requires to perform a CAC first.
905 channel
= dfs_get_valid_channel(iface
, &secondary_channel
,
906 &vht_oper_centr_freq_seg0_idx
,
907 &vht_oper_centr_freq_seg1_idx
,
911 "%s: no DFS channels left, waiting for NOP to finish",
916 iface
->freq
= channel
->freq
;
917 iface
->conf
->channel
= channel
->chan
;
918 iface
->conf
->secondary_channel
= secondary_channel
;
919 iface
->conf
->vht_oper_centr_freq_seg0_idx
=
920 vht_oper_centr_freq_seg0_idx
;
921 iface
->conf
->vht_oper_centr_freq_seg1_idx
=
922 vht_oper_centr_freq_seg1_idx
;
924 hostapd_disable_iface(iface
);
925 hostapd_enable_iface(iface
);
929 wpa_printf(MSG_DEBUG
, "DFS will switch to a new channel %d",
931 wpa_msg(iface
->bss
[0]->msg_ctx
, MSG_INFO
, DFS_EVENT_NEW_CHANNEL
932 "freq=%d chan=%d sec_chan=%d", channel
->freq
,
933 channel
->chan
, secondary_channel
);
935 /* Setup CSA request */
936 os_memset(&csa_settings
, 0, sizeof(csa_settings
));
937 csa_settings
.cs_count
= 5;
938 csa_settings
.block_tx
= 1;
939 err
= hostapd_set_freq_params(&csa_settings
.freq_params
,
940 iface
->conf
->hw_mode
,
943 iface
->conf
->ieee80211n
,
944 iface
->conf
->ieee80211ac
,
946 iface
->conf
->vht_oper_chwidth
,
947 vht_oper_centr_freq_seg0_idx
,
948 vht_oper_centr_freq_seg1_idx
,
949 iface
->current_mode
->vht_capab
);
952 wpa_printf(MSG_ERROR
, "DFS failed to calculate CSA freq params");
953 hostapd_disable_iface(iface
);
957 for (i
= 0; i
< iface
->num_bss
; i
++) {
958 err
= hostapd_switch_channel(iface
->bss
[i
], &csa_settings
);
964 wpa_printf(MSG_WARNING
, "DFS failed to schedule CSA (%d) - trying fallback",
966 iface
->freq
= channel
->freq
;
967 iface
->conf
->channel
= channel
->chan
;
968 iface
->conf
->secondary_channel
= secondary_channel
;
969 iface
->conf
->vht_oper_centr_freq_seg0_idx
=
970 vht_oper_centr_freq_seg0_idx
;
971 iface
->conf
->vht_oper_centr_freq_seg1_idx
=
972 vht_oper_centr_freq_seg1_idx
;
974 hostapd_disable_iface(iface
);
975 hostapd_enable_iface(iface
);
979 /* Channel configuration will be updated once CSA completes and
980 * ch_switch_notify event is received */
982 wpa_printf(MSG_DEBUG
, "DFS waiting channel switch event");
987 int hostapd_dfs_radar_detected(struct hostapd_iface
*iface
, int freq
,
988 int ht_enabled
, int chan_offset
, int chan_width
,
993 wpa_msg(iface
->bss
[0]->msg_ctx
, MSG_INFO
, DFS_EVENT_RADAR_DETECTED
994 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
995 freq
, ht_enabled
, chan_offset
, chan_width
, cf1
, cf2
);
997 /* Proceed only if DFS is not offloaded to the driver */
998 if (iface
->drv_flags
& WPA_DRIVER_FLAGS_DFS_OFFLOAD
)
1001 if (!iface
->conf
->ieee80211h
)
1004 /* mark radar frequency as invalid */
1005 set_dfs_state(iface
, freq
, ht_enabled
, chan_offset
, chan_width
,
1006 cf1
, cf2
, HOSTAPD_CHAN_DFS_UNAVAILABLE
);
1008 /* Skip if reported radar event not overlapped our channels */
1009 res
= dfs_are_channels_overlapped(iface
, freq
, chan_width
, cf1
, cf2
);
1013 /* radar detected while operating, switch the channel. */
1014 res
= hostapd_dfs_start_channel_switch(iface
);
1020 int hostapd_dfs_nop_finished(struct hostapd_iface
*iface
, int freq
,
1021 int ht_enabled
, int chan_offset
, int chan_width
,
1024 wpa_msg(iface
->bss
[0]->msg_ctx
, MSG_INFO
, DFS_EVENT_NOP_FINISHED
1025 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
1026 freq
, ht_enabled
, chan_offset
, chan_width
, cf1
, cf2
);
1028 /* Proceed only if DFS is not offloaded to the driver */
1029 if (iface
->drv_flags
& WPA_DRIVER_FLAGS_DFS_OFFLOAD
)
1032 /* TODO add correct implementation here */
1033 set_dfs_state(iface
, freq
, ht_enabled
, chan_offset
, chan_width
,
1034 cf1
, cf2
, HOSTAPD_CHAN_DFS_USABLE
);
1036 /* Handle cases where all channels were initially unavailable */
1037 if (iface
->state
== HAPD_IFACE_DFS
&& !iface
->cac_started
)
1038 hostapd_handle_dfs(iface
);
1044 int hostapd_is_dfs_required(struct hostapd_iface
*iface
)
1046 int n_chans
, n_chans1
, start_chan_idx
, start_chan_idx1
, res
;
1048 if (!iface
->conf
->ieee80211h
|| !iface
->current_mode
||
1049 iface
->current_mode
->mode
!= HOSTAPD_MODE_IEEE80211A
)
1052 /* Get start (first) channel for current configuration */
1053 start_chan_idx
= dfs_get_start_chan_idx(iface
, &start_chan_idx1
);
1054 if (start_chan_idx
== -1)
1057 /* Get number of used channels, depend on width */
1058 n_chans
= dfs_get_used_n_chans(iface
, &n_chans1
);
1060 /* Check if any of configured channels require DFS */
1061 res
= dfs_check_chans_radar(iface
, start_chan_idx
, n_chans
);
1064 if (start_chan_idx1
>= 0 && n_chans1
> 0)
1065 res
= dfs_check_chans_radar(iface
, start_chan_idx1
, n_chans1
);
1070 int hostapd_dfs_start_cac(struct hostapd_iface
*iface
, int freq
,
1071 int ht_enabled
, int chan_offset
, int chan_width
,
1074 wpa_msg(iface
->bss
[0]->msg_ctx
, MSG_INFO
, DFS_EVENT_CAC_START
1075 "freq=%d chan=%d chan_offset=%d width=%d seg0=%d "
1076 "seg1=%d cac_time=%ds",
1077 freq
, (freq
- 5000) / 5, chan_offset
, chan_width
, cf1
, cf2
, 60);
1078 iface
->cac_started
= 1;
1084 * Main DFS handler for offloaded case.
1085 * 2 - continue channel/AP setup for non-DFS channel
1086 * 1 - continue channel/AP setup for DFS channel
1087 * 0 - channel/AP setup will be continued after CAC
1088 * -1 - hit critical error
1090 int hostapd_handle_dfs_offload(struct hostapd_iface
*iface
)
1092 wpa_printf(MSG_DEBUG
, "%s: iface->cac_started: %d",
1093 __func__
, iface
->cac_started
);
1096 * If DFS has already been started, then we are being called from a
1097 * callback to continue AP/channel setup. Reset the CAC start flag and
1100 if (iface
->cac_started
) {
1101 wpa_printf(MSG_DEBUG
, "%s: iface->cac_started: %d",
1102 __func__
, iface
->cac_started
);
1103 iface
->cac_started
= 0;
1107 if (ieee80211_is_dfs(iface
->freq
)) {
1108 wpa_printf(MSG_DEBUG
, "%s: freq %d MHz requires DFS",
1109 __func__
, iface
->freq
);
1113 wpa_printf(MSG_DEBUG
,
1114 "%s: freq %d MHz does not require DFS. Continue channel/AP setup",
1115 __func__
, iface
->freq
);