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
|| iface
->conf
->ieee80211ax
) {
32 switch (hostapd_get_oper_chwidth(iface
->conf
)) {
33 case CHANWIDTH_USE_HT
:
38 case CHANWIDTH_160MHZ
:
41 case 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
;
145 u32 bw
= num_chan_to_bw(num_chans
);
147 if (first_chan_idx
+ num_chans
> mode
->num_channels
)
150 first_chan
= &mode
->channels
[first_chan_idx
];
152 /* hostapd DFS implementation assumes the first channel as primary.
153 * If it's not allowed to use the first channel as primary, decline the
154 * whole channel range. */
155 if (!chan_pri_allowed(first_chan
))
158 for (i
= 0; i
< num_chans
; i
++) {
159 chan
= dfs_get_chan_data(mode
, first_chan
->freq
+ i
* 20,
164 /* HT 40 MHz secondary channel availability checked only for
166 if (!chan_bw_allowed(chan
, bw
, 1, !i
))
169 if (!dfs_channel_available(chan
, skip_radar
))
177 static int is_in_chanlist(struct hostapd_iface
*iface
,
178 struct hostapd_channel_data
*chan
)
180 if (!iface
->conf
->acs_ch_list
.num
)
183 return freq_range_list_includes(&iface
->conf
->acs_ch_list
, chan
->chan
);
188 * The function assumes HT40+ operation.
189 * Make sure to adjust the following variables after calling this:
190 * - hapd->secondary_channel
191 * - hapd->vht/he_oper_centr_freq_seg0_idx
192 * - hapd->vht/he_oper_centr_freq_seg1_idx
194 static int dfs_find_channel(struct hostapd_iface
*iface
,
195 struct hostapd_channel_data
**ret_chan
,
196 int idx
, int skip_radar
)
198 struct hostapd_hw_modes
*mode
;
199 struct hostapd_channel_data
*chan
;
200 int i
, channel_idx
= 0, n_chans
, n_chans1
;
202 mode
= iface
->current_mode
;
203 n_chans
= dfs_get_used_n_chans(iface
, &n_chans1
);
205 wpa_printf(MSG_DEBUG
, "DFS new chan checking %d channels", n_chans
);
206 for (i
= 0; i
< mode
->num_channels
; i
++) {
207 chan
= &mode
->channels
[i
];
209 /* Skip HT40/VHT incompatible channels */
210 if (iface
->conf
->ieee80211n
&&
211 iface
->conf
->secondary_channel
&&
212 (!dfs_is_chan_allowed(chan
, n_chans
) ||
213 !(chan
->allowed_bw
& HOSTAPD_CHAN_WIDTH_40P
)))
216 /* Skip incompatible chandefs */
217 if (!dfs_chan_range_available(mode
, i
, n_chans
, skip_radar
))
220 if (!is_in_chanlist(iface
, chan
))
223 if (ret_chan
&& idx
== channel_idx
) {
224 wpa_printf(MSG_DEBUG
, "Selected ch. #%d", chan
->chan
);
228 wpa_printf(MSG_DEBUG
, "Adding channel: %d", chan
->chan
);
235 static void dfs_adjust_center_freq(struct hostapd_iface
*iface
,
236 struct hostapd_channel_data
*chan
,
237 int secondary_channel
,
238 u8
*oper_centr_freq_seg0_idx
,
239 u8
*oper_centr_freq_seg1_idx
)
241 if (!iface
->conf
->ieee80211ac
&& !iface
->conf
->ieee80211ax
)
247 *oper_centr_freq_seg1_idx
= 0;
249 switch (hostapd_get_oper_chwidth(iface
->conf
)) {
250 case CHANWIDTH_USE_HT
:
251 if (secondary_channel
== 1)
252 *oper_centr_freq_seg0_idx
= chan
->chan
+ 2;
253 else if (secondary_channel
== -1)
254 *oper_centr_freq_seg0_idx
= chan
->chan
- 2;
256 *oper_centr_freq_seg0_idx
= chan
->chan
;
258 case CHANWIDTH_80MHZ
:
259 *oper_centr_freq_seg0_idx
= chan
->chan
+ 6;
261 case CHANWIDTH_160MHZ
:
262 *oper_centr_freq_seg0_idx
= chan
->chan
+ 14;
265 wpa_printf(MSG_INFO
, "DFS only VHT20/40/80/160 is supported now");
266 *oper_centr_freq_seg0_idx
= 0;
270 wpa_printf(MSG_DEBUG
, "DFS adjusting VHT center frequency: %d, %d",
271 *oper_centr_freq_seg0_idx
,
272 *oper_centr_freq_seg1_idx
);
276 /* Return start channel idx we will use for mode->channels[idx] */
277 static int dfs_get_start_chan_idx(struct hostapd_iface
*iface
, int *seg1_start
)
279 struct hostapd_hw_modes
*mode
;
280 struct hostapd_channel_data
*chan
;
281 int channel_no
= iface
->conf
->channel
;
288 if (iface
->conf
->ieee80211n
&& iface
->conf
->secondary_channel
== -1)
292 if (iface
->conf
->ieee80211ac
|| iface
->conf
->ieee80211ax
) {
293 switch (hostapd_get_oper_chwidth(iface
->conf
)) {
294 case CHANWIDTH_USE_HT
:
296 case CHANWIDTH_80MHZ
:
297 channel_no
= hostapd_get_oper_centr_freq_seg0_idx(
300 case CHANWIDTH_160MHZ
:
301 channel_no
= hostapd_get_oper_centr_freq_seg0_idx(
304 case CHANWIDTH_80P80MHZ
:
305 channel_no
= hostapd_get_oper_centr_freq_seg0_idx(
307 chan_seg1
= hostapd_get_oper_centr_freq_seg1_idx(
312 "DFS only VHT20/40/80/160/80+80 is supported now");
319 mode
= iface
->current_mode
;
320 for (i
= 0; i
< mode
->num_channels
; i
++) {
321 chan
= &mode
->channels
[i
];
322 if (chan
->chan
== channel_no
) {
328 if (res
!= -1 && chan_seg1
> -1) {
331 /* Get idx for seg1 */
332 mode
= iface
->current_mode
;
333 for (i
= 0; i
< mode
->num_channels
; i
++) {
334 chan
= &mode
->channels
[i
];
335 if (chan
->chan
== chan_seg1
) {
346 wpa_printf(MSG_DEBUG
,
347 "DFS chan_idx seems wrong; num-ch: %d ch-no: %d conf-ch-no: %d 11n: %d sec-ch: %d vht-oper-width: %d",
348 mode
->num_channels
, channel_no
, iface
->conf
->channel
,
349 iface
->conf
->ieee80211n
,
350 iface
->conf
->secondary_channel
,
351 hostapd_get_oper_chwidth(iface
->conf
));
353 for (i
= 0; i
< mode
->num_channels
; i
++) {
354 wpa_printf(MSG_DEBUG
, "Available channel: %d",
355 mode
->channels
[i
].chan
);
363 /* At least one channel have radar flag */
364 static int dfs_check_chans_radar(struct hostapd_iface
*iface
,
365 int start_chan_idx
, int n_chans
)
367 struct hostapd_channel_data
*channel
;
368 struct hostapd_hw_modes
*mode
;
371 mode
= iface
->current_mode
;
373 for (i
= 0; i
< n_chans
; i
++) {
374 channel
= &mode
->channels
[start_chan_idx
+ i
];
375 if (channel
->flag
& HOSTAPD_CHAN_RADAR
)
383 /* All channels available */
384 static int dfs_check_chans_available(struct hostapd_iface
*iface
,
385 int start_chan_idx
, int n_chans
)
387 struct hostapd_channel_data
*channel
;
388 struct hostapd_hw_modes
*mode
;
391 mode
= iface
->current_mode
;
393 for (i
= 0; i
< n_chans
; i
++) {
394 channel
= &mode
->channels
[start_chan_idx
+ i
];
396 if (channel
->flag
& HOSTAPD_CHAN_DISABLED
)
399 if (!(channel
->flag
& HOSTAPD_CHAN_RADAR
))
402 if ((channel
->flag
& HOSTAPD_CHAN_DFS_MASK
) !=
403 HOSTAPD_CHAN_DFS_AVAILABLE
)
411 /* At least one channel unavailable */
412 static int dfs_check_chans_unavailable(struct hostapd_iface
*iface
,
416 struct hostapd_channel_data
*channel
;
417 struct hostapd_hw_modes
*mode
;
420 mode
= iface
->current_mode
;
422 for (i
= 0; i
< n_chans
; i
++) {
423 channel
= &mode
->channels
[start_chan_idx
+ i
];
424 if (channel
->flag
& HOSTAPD_CHAN_DISABLED
)
426 if ((channel
->flag
& HOSTAPD_CHAN_DFS_MASK
) ==
427 HOSTAPD_CHAN_DFS_UNAVAILABLE
)
435 static struct hostapd_channel_data
*
436 dfs_get_valid_channel(struct hostapd_iface
*iface
,
437 int *secondary_channel
,
438 u8
*oper_centr_freq_seg0_idx
,
439 u8
*oper_centr_freq_seg1_idx
,
442 struct hostapd_hw_modes
*mode
;
443 struct hostapd_channel_data
*chan
= NULL
;
444 int num_available_chandefs
;
448 wpa_printf(MSG_DEBUG
, "DFS: Selecting random channel");
449 *secondary_channel
= 0;
450 *oper_centr_freq_seg0_idx
= 0;
451 *oper_centr_freq_seg1_idx
= 0;
453 if (iface
->current_mode
== NULL
)
456 mode
= iface
->current_mode
;
457 if (mode
->mode
!= HOSTAPD_MODE_IEEE80211A
)
460 /* Get the count first */
461 num_available_chandefs
= dfs_find_channel(iface
, NULL
, 0, skip_radar
);
462 if (num_available_chandefs
== 0)
465 if (os_get_random((u8
*) &_rand
, sizeof(_rand
)) < 0)
467 chan_idx
= _rand
% num_available_chandefs
;
468 dfs_find_channel(iface
, &chan
, chan_idx
, skip_radar
);
470 /* dfs_find_channel() calculations assume HT40+ */
471 if (iface
->conf
->secondary_channel
)
472 *secondary_channel
= 1;
474 *secondary_channel
= 0;
476 dfs_adjust_center_freq(iface
, chan
,
478 oper_centr_freq_seg0_idx
,
479 oper_centr_freq_seg1_idx
);
485 static int set_dfs_state_freq(struct hostapd_iface
*iface
, int freq
, u32 state
)
487 struct hostapd_hw_modes
*mode
;
488 struct hostapd_channel_data
*chan
= NULL
;
491 mode
= iface
->current_mode
;
495 wpa_printf(MSG_DEBUG
, "set_dfs_state 0x%X for %d MHz", state
, freq
);
496 for (i
= 0; i
< iface
->current_mode
->num_channels
; i
++) {
497 chan
= &iface
->current_mode
->channels
[i
];
498 if (chan
->freq
== freq
) {
499 if (chan
->flag
& HOSTAPD_CHAN_RADAR
) {
500 chan
->flag
&= ~HOSTAPD_CHAN_DFS_MASK
;
502 return 1; /* Channel found */
506 wpa_printf(MSG_WARNING
, "Can't set DFS state for freq %d MHz", freq
);
511 static int set_dfs_state(struct hostapd_iface
*iface
, int freq
, int ht_enabled
,
512 int chan_offset
, int chan_width
, int cf1
,
516 struct hostapd_hw_modes
*mode
;
517 int frequency
= freq
;
521 mode
= iface
->current_mode
;
525 if (mode
->mode
!= HOSTAPD_MODE_IEEE80211A
) {
526 wpa_printf(MSG_WARNING
, "current_mode != IEEE80211A");
530 /* Seems cf1 and chan_width is enough here */
531 switch (chan_width
) {
532 case CHAN_WIDTH_20_NOHT
:
540 frequency
= cf1
- 10;
544 frequency
= cf1
- 30;
546 case CHAN_WIDTH_80P80
:
548 frequency
= cf1
- 30;
549 frequency2
= cf2
- 30;
553 frequency
= cf1
- 70;
556 wpa_printf(MSG_INFO
, "DFS chan_width %d not supported",
561 wpa_printf(MSG_DEBUG
, "DFS freq: %dMHz, n_chans: %d", frequency
,
563 for (i
= 0; i
< n_chans
; i
++) {
564 ret
+= set_dfs_state_freq(iface
, frequency
, state
);
565 frequency
= frequency
+ 20;
567 if (chan_width
== CHAN_WIDTH_80P80
) {
568 ret
+= set_dfs_state_freq(iface
, frequency2
, state
);
569 frequency2
= frequency2
+ 20;
577 static int dfs_are_channels_overlapped(struct hostapd_iface
*iface
, int freq
,
578 int chan_width
, int cf1
, int cf2
)
580 int start_chan_idx
, start_chan_idx1
;
581 struct hostapd_hw_modes
*mode
;
582 struct hostapd_channel_data
*chan
;
583 int n_chans
, n_chans1
, i
, j
, frequency
= freq
, radar_n_chans
= 1;
587 /* Our configuration */
588 mode
= iface
->current_mode
;
589 start_chan_idx
= dfs_get_start_chan_idx(iface
, &start_chan_idx1
);
590 n_chans
= dfs_get_used_n_chans(iface
, &n_chans1
);
592 /* Check we are on DFS channel(s) */
593 if (!dfs_check_chans_radar(iface
, start_chan_idx
, n_chans
))
596 /* Reported via radar event */
597 switch (chan_width
) {
598 case CHAN_WIDTH_20_NOHT
:
606 frequency
= cf1
- 10;
610 frequency
= cf1
- 30;
614 frequency
= cf1
- 70;
617 wpa_printf(MSG_INFO
, "DFS chan_width %d not supported",
622 ieee80211_freq_to_chan(frequency
, &radar_chan
);
624 for (i
= 0; i
< n_chans
; i
++) {
625 chan
= &mode
->channels
[start_chan_idx
+ i
];
626 if (!(chan
->flag
& HOSTAPD_CHAN_RADAR
))
628 for (j
= 0; j
< radar_n_chans
; j
++) {
629 wpa_printf(MSG_DEBUG
, "checking our: %d, radar: %d",
630 chan
->chan
, radar_chan
+ j
* 4);
631 if (chan
->chan
== radar_chan
+ j
* 4)
636 wpa_printf(MSG_DEBUG
, "overlapped: %d", res
);
642 static unsigned int dfs_get_cac_time(struct hostapd_iface
*iface
,
643 int start_chan_idx
, int n_chans
)
645 struct hostapd_channel_data
*channel
;
646 struct hostapd_hw_modes
*mode
;
648 unsigned int cac_time_ms
= 0;
650 mode
= iface
->current_mode
;
652 for (i
= 0; i
< n_chans
; i
++) {
653 channel
= &mode
->channels
[start_chan_idx
+ i
];
654 if (!(channel
->flag
& HOSTAPD_CHAN_RADAR
))
656 if (channel
->dfs_cac_ms
> cac_time_ms
)
657 cac_time_ms
= channel
->dfs_cac_ms
;
666 * 1 - continue channel/ap setup
667 * 0 - channel/ap setup will be continued after CAC
668 * -1 - hit critical error
670 int hostapd_handle_dfs(struct hostapd_iface
*iface
)
672 struct hostapd_channel_data
*channel
;
673 int res
, n_chans
, n_chans1
, start_chan_idx
, start_chan_idx1
;
676 if (is_6ghz_freq(iface
->freq
))
679 if (!iface
->current_mode
) {
681 * This can happen with drivers that do not provide mode
682 * information and as such, cannot really use hostapd for DFS.
684 wpa_printf(MSG_DEBUG
,
685 "DFS: No current_mode information - assume no need to perform DFS operations by hostapd");
689 iface
->cac_started
= 0;
692 /* Get start (first) channel for current configuration */
693 start_chan_idx
= dfs_get_start_chan_idx(iface
,
695 if (start_chan_idx
== -1)
698 /* Get number of used channels, depend on width */
699 n_chans
= dfs_get_used_n_chans(iface
, &n_chans1
);
702 iface
->dfs_cac_ms
= dfs_get_cac_time(iface
, start_chan_idx
,
705 /* Check if any of configured channels require DFS */
706 res
= dfs_check_chans_radar(iface
, start_chan_idx
, n_chans
);
707 wpa_printf(MSG_DEBUG
,
708 "DFS %d channels required radar detection",
713 /* Check if all channels are DFS available */
714 res
= dfs_check_chans_available(iface
, start_chan_idx
, n_chans
);
715 wpa_printf(MSG_DEBUG
,
716 "DFS all channels available, (SKIP CAC): %s",
721 /* Check if any of configured channels is unavailable */
722 res
= dfs_check_chans_unavailable(iface
, start_chan_idx
,
724 wpa_printf(MSG_DEBUG
, "DFS %d chans unavailable - choose other channel: %s",
725 res
, res
? "yes": "no");
730 channel
= dfs_get_valid_channel(iface
, &sec
, &cf1
, &cf2
,
733 wpa_printf(MSG_ERROR
, "could not get valid channel");
734 hostapd_set_state(iface
, HAPD_IFACE_DFS
);
738 iface
->freq
= channel
->freq
;
739 iface
->conf
->channel
= channel
->chan
;
740 iface
->conf
->secondary_channel
= sec
;
741 hostapd_set_oper_centr_freq_seg0_idx(iface
->conf
, cf1
);
742 hostapd_set_oper_centr_freq_seg1_idx(iface
->conf
, cf2
);
746 /* Finally start CAC */
747 hostapd_set_state(iface
, HAPD_IFACE_DFS
);
748 wpa_printf(MSG_DEBUG
, "DFS start CAC on %d MHz", iface
->freq
);
749 wpa_msg(iface
->bss
[0]->msg_ctx
, MSG_INFO
, DFS_EVENT_CAC_START
750 "freq=%d chan=%d sec_chan=%d, width=%d, seg0=%d, seg1=%d, cac_time=%ds",
752 iface
->conf
->channel
, iface
->conf
->secondary_channel
,
753 hostapd_get_oper_chwidth(iface
->conf
),
754 hostapd_get_oper_centr_freq_seg0_idx(iface
->conf
),
755 hostapd_get_oper_centr_freq_seg1_idx(iface
->conf
),
756 iface
->dfs_cac_ms
/ 1000);
758 res
= hostapd_start_dfs_cac(
759 iface
, iface
->conf
->hw_mode
, iface
->freq
, iface
->conf
->channel
,
760 iface
->conf
->ieee80211n
, iface
->conf
->ieee80211ac
,
761 iface
->conf
->ieee80211ax
,
762 iface
->conf
->secondary_channel
,
763 hostapd_get_oper_chwidth(iface
->conf
),
764 hostapd_get_oper_centr_freq_seg0_idx(iface
->conf
),
765 hostapd_get_oper_centr_freq_seg1_idx(iface
->conf
));
768 wpa_printf(MSG_ERROR
, "DFS start_dfs_cac() failed, %d", res
);
776 static int hostapd_config_dfs_chan_available(struct hostapd_iface
*iface
)
778 int n_chans
, n_chans1
, start_chan_idx
, start_chan_idx1
;
780 /* Get the start (first) channel for current configuration */
781 start_chan_idx
= dfs_get_start_chan_idx(iface
, &start_chan_idx1
);
782 if (start_chan_idx
< 0)
785 /* Get the number of used channels, depending on width */
786 n_chans
= dfs_get_used_n_chans(iface
, &n_chans1
);
788 /* Check if all channels are DFS available */
789 return dfs_check_chans_available(iface
, start_chan_idx
, n_chans
);
793 int hostapd_dfs_complete_cac(struct hostapd_iface
*iface
, int success
, int freq
,
794 int ht_enabled
, int chan_offset
, int chan_width
,
797 wpa_msg(iface
->bss
[0]->msg_ctx
, MSG_INFO
, DFS_EVENT_CAC_COMPLETED
798 "success=%d freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
799 success
, freq
, ht_enabled
, chan_offset
, chan_width
, cf1
, cf2
);
802 /* Complete iface/ap configuration */
803 if (iface
->drv_flags
& WPA_DRIVER_FLAGS_DFS_OFFLOAD
) {
804 /* Complete AP configuration for the first bring up. */
805 if (iface
->state
!= HAPD_IFACE_ENABLED
)
806 hostapd_setup_interface_complete(iface
, 0);
808 iface
->cac_started
= 0;
810 set_dfs_state(iface
, freq
, ht_enabled
, chan_offset
,
811 chan_width
, cf1
, cf2
,
812 HOSTAPD_CHAN_DFS_AVAILABLE
);
814 * Just mark the channel available when CAC completion
815 * event is received in enabled state. CAC result could
816 * have been propagated from another radio having the
817 * same regulatory configuration. When CAC completion is
818 * received during non-HAPD_IFACE_ENABLED state, make
819 * sure the configured channel is available because this
820 * CAC completion event could have been propagated from
823 if (iface
->state
!= HAPD_IFACE_ENABLED
&&
824 hostapd_config_dfs_chan_available(iface
)) {
825 hostapd_setup_interface_complete(iface
, 0);
826 iface
->cac_started
= 0;
835 int hostapd_dfs_pre_cac_expired(struct hostapd_iface
*iface
, int freq
,
836 int ht_enabled
, int chan_offset
, int chan_width
,
839 wpa_msg(iface
->bss
[0]->msg_ctx
, MSG_INFO
, DFS_EVENT_PRE_CAC_EXPIRED
840 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
841 freq
, ht_enabled
, chan_offset
, chan_width
, cf1
, cf2
);
843 /* Proceed only if DFS is not offloaded to the driver */
844 if (iface
->drv_flags
& WPA_DRIVER_FLAGS_DFS_OFFLOAD
)
847 set_dfs_state(iface
, freq
, ht_enabled
, chan_offset
, chan_width
,
848 cf1
, cf2
, HOSTAPD_CHAN_DFS_USABLE
);
854 static int hostapd_dfs_start_channel_switch_cac(struct hostapd_iface
*iface
)
856 struct hostapd_channel_data
*channel
;
857 int secondary_channel
;
858 u8 oper_centr_freq_seg0_idx
= 0;
859 u8 oper_centr_freq_seg1_idx
= 0;
863 /* Radar detected during active CAC */
864 iface
->cac_started
= 0;
865 channel
= dfs_get_valid_channel(iface
, &secondary_channel
,
866 &oper_centr_freq_seg0_idx
,
867 &oper_centr_freq_seg1_idx
,
871 wpa_printf(MSG_ERROR
, "No valid channel available");
875 wpa_printf(MSG_DEBUG
, "DFS will switch to a new channel %d",
877 wpa_msg(iface
->bss
[0]->msg_ctx
, MSG_INFO
, DFS_EVENT_NEW_CHANNEL
878 "freq=%d chan=%d sec_chan=%d", channel
->freq
,
879 channel
->chan
, secondary_channel
);
881 iface
->freq
= channel
->freq
;
882 iface
->conf
->channel
= channel
->chan
;
883 iface
->conf
->secondary_channel
= secondary_channel
;
884 hostapd_set_oper_centr_freq_seg0_idx(iface
->conf
,
885 oper_centr_freq_seg0_idx
);
886 hostapd_set_oper_centr_freq_seg1_idx(iface
->conf
,
887 oper_centr_freq_seg1_idx
);
890 hostapd_setup_interface_complete(iface
, err
);
895 static int hostapd_dfs_start_channel_switch(struct hostapd_iface
*iface
)
897 struct hostapd_channel_data
*channel
;
898 int secondary_channel
;
899 u8 oper_centr_freq_seg0_idx
;
900 u8 oper_centr_freq_seg1_idx
;
902 struct csa_settings csa_settings
;
905 struct hostapd_hw_modes
*cmode
= iface
->current_mode
;
907 wpa_printf(MSG_DEBUG
, "%s called (CAC active: %s, CSA active: %s)",
908 __func__
, iface
->cac_started
? "yes" : "no",
909 hostapd_csa_in_progress(iface
) ? "yes" : "no");
911 /* Check if CSA in progress */
912 if (hostapd_csa_in_progress(iface
))
915 /* Check if active CAC */
916 if (iface
->cac_started
)
917 return hostapd_dfs_start_channel_switch_cac(iface
);
920 * Allow selection of DFS channel in ETSI to comply with
923 if (iface
->dfs_domain
== HOSTAPD_DFS_REGION_ETSI
)
926 /* Perform channel switch/CSA */
927 channel
= dfs_get_valid_channel(iface
, &secondary_channel
,
928 &oper_centr_freq_seg0_idx
,
929 &oper_centr_freq_seg1_idx
,
934 * If there is no channel to switch immediately to, check if
935 * there is another channel where we can switch even if it
936 * requires to perform a CAC first.
939 channel
= dfs_get_valid_channel(iface
, &secondary_channel
,
940 &oper_centr_freq_seg0_idx
,
941 &oper_centr_freq_seg1_idx
,
945 "%s: no DFS channels left, waiting for NOP to finish",
950 iface
->freq
= channel
->freq
;
951 iface
->conf
->channel
= channel
->chan
;
952 iface
->conf
->secondary_channel
= secondary_channel
;
953 hostapd_set_oper_centr_freq_seg0_idx(iface
->conf
,
954 oper_centr_freq_seg0_idx
);
955 hostapd_set_oper_centr_freq_seg1_idx(iface
->conf
,
956 oper_centr_freq_seg1_idx
);
958 hostapd_disable_iface(iface
);
959 hostapd_enable_iface(iface
);
963 wpa_printf(MSG_DEBUG
, "DFS will switch to a new channel %d",
965 wpa_msg(iface
->bss
[0]->msg_ctx
, MSG_INFO
, DFS_EVENT_NEW_CHANNEL
966 "freq=%d chan=%d sec_chan=%d", channel
->freq
,
967 channel
->chan
, secondary_channel
);
969 /* Setup CSA request */
970 os_memset(&csa_settings
, 0, sizeof(csa_settings
));
971 csa_settings
.cs_count
= 5;
972 csa_settings
.block_tx
= 1;
973 err
= hostapd_set_freq_params(&csa_settings
.freq_params
,
974 iface
->conf
->hw_mode
,
977 iface
->conf
->enable_edmg
,
978 iface
->conf
->edmg_channel
,
979 iface
->conf
->ieee80211n
,
980 iface
->conf
->ieee80211ac
,
981 iface
->conf
->ieee80211ax
,
983 hostapd_get_oper_chwidth(iface
->conf
),
984 oper_centr_freq_seg0_idx
,
985 oper_centr_freq_seg1_idx
,
987 &cmode
->he_capab
[IEEE80211_MODE_AP
]);
990 wpa_printf(MSG_ERROR
, "DFS failed to calculate CSA freq params");
991 hostapd_disable_iface(iface
);
995 for (i
= 0; i
< iface
->num_bss
; i
++) {
996 err
= hostapd_switch_channel(iface
->bss
[i
], &csa_settings
);
1002 wpa_printf(MSG_WARNING
, "DFS failed to schedule CSA (%d) - trying fallback",
1004 iface
->freq
= channel
->freq
;
1005 iface
->conf
->channel
= channel
->chan
;
1006 iface
->conf
->secondary_channel
= secondary_channel
;
1007 hostapd_set_oper_centr_freq_seg0_idx(iface
->conf
,
1008 oper_centr_freq_seg0_idx
);
1009 hostapd_set_oper_centr_freq_seg1_idx(iface
->conf
,
1010 oper_centr_freq_seg1_idx
);
1012 hostapd_disable_iface(iface
);
1013 hostapd_enable_iface(iface
);
1017 /* Channel configuration will be updated once CSA completes and
1018 * ch_switch_notify event is received */
1020 wpa_printf(MSG_DEBUG
, "DFS waiting channel switch event");
1025 int hostapd_dfs_radar_detected(struct hostapd_iface
*iface
, int freq
,
1026 int ht_enabled
, int chan_offset
, int chan_width
,
1031 wpa_msg(iface
->bss
[0]->msg_ctx
, MSG_INFO
, DFS_EVENT_RADAR_DETECTED
1032 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
1033 freq
, ht_enabled
, chan_offset
, chan_width
, cf1
, cf2
);
1035 /* Proceed only if DFS is not offloaded to the driver */
1036 if (iface
->drv_flags
& WPA_DRIVER_FLAGS_DFS_OFFLOAD
)
1039 if (!iface
->conf
->ieee80211h
)
1042 /* mark radar frequency as invalid */
1043 res
= set_dfs_state(iface
, freq
, ht_enabled
, chan_offset
, chan_width
,
1044 cf1
, cf2
, HOSTAPD_CHAN_DFS_UNAVAILABLE
);
1048 /* Skip if reported radar event not overlapped our channels */
1049 res
= dfs_are_channels_overlapped(iface
, freq
, chan_width
, cf1
, cf2
);
1053 /* radar detected while operating, switch the channel. */
1054 res
= hostapd_dfs_start_channel_switch(iface
);
1060 int hostapd_dfs_nop_finished(struct hostapd_iface
*iface
, int freq
,
1061 int ht_enabled
, int chan_offset
, int chan_width
,
1064 wpa_msg(iface
->bss
[0]->msg_ctx
, MSG_INFO
, DFS_EVENT_NOP_FINISHED
1065 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
1066 freq
, ht_enabled
, chan_offset
, chan_width
, cf1
, cf2
);
1068 /* Proceed only if DFS is not offloaded to the driver */
1069 if (iface
->drv_flags
& WPA_DRIVER_FLAGS_DFS_OFFLOAD
)
1072 /* TODO add correct implementation here */
1073 set_dfs_state(iface
, freq
, ht_enabled
, chan_offset
, chan_width
,
1074 cf1
, cf2
, HOSTAPD_CHAN_DFS_USABLE
);
1076 /* Handle cases where all channels were initially unavailable */
1077 if (iface
->state
== HAPD_IFACE_DFS
&& !iface
->cac_started
)
1078 hostapd_handle_dfs(iface
);
1084 int hostapd_is_dfs_required(struct hostapd_iface
*iface
)
1086 int n_chans
, n_chans1
, start_chan_idx
, start_chan_idx1
, res
;
1088 if (!iface
->conf
->ieee80211h
|| !iface
->current_mode
||
1089 iface
->current_mode
->mode
!= HOSTAPD_MODE_IEEE80211A
)
1092 /* Get start (first) channel for current configuration */
1093 start_chan_idx
= dfs_get_start_chan_idx(iface
, &start_chan_idx1
);
1094 if (start_chan_idx
== -1)
1097 /* Get number of used channels, depend on width */
1098 n_chans
= dfs_get_used_n_chans(iface
, &n_chans1
);
1100 /* Check if any of configured channels require DFS */
1101 res
= dfs_check_chans_radar(iface
, start_chan_idx
, n_chans
);
1104 if (start_chan_idx1
>= 0 && n_chans1
> 0)
1105 res
= dfs_check_chans_radar(iface
, start_chan_idx1
, n_chans1
);
1110 int hostapd_dfs_start_cac(struct hostapd_iface
*iface
, int freq
,
1111 int ht_enabled
, int chan_offset
, int chan_width
,
1114 /* This is called when the driver indicates that an offloaded DFS has
1116 hostapd_set_state(iface
, HAPD_IFACE_DFS
);
1117 /* TODO: How to check CAC time for ETSI weather channels? */
1118 iface
->dfs_cac_ms
= 60000;
1119 wpa_msg(iface
->bss
[0]->msg_ctx
, MSG_INFO
, DFS_EVENT_CAC_START
1120 "freq=%d chan=%d chan_offset=%d width=%d seg0=%d "
1121 "seg1=%d cac_time=%ds",
1122 freq
, (freq
- 5000) / 5, chan_offset
, chan_width
, cf1
, cf2
,
1123 iface
->dfs_cac_ms
/ 1000);
1124 iface
->cac_started
= 1;
1125 os_get_reltime(&iface
->dfs_cac_start
);
1131 * Main DFS handler for offloaded case.
1132 * 2 - continue channel/AP setup for non-DFS channel
1133 * 1 - continue channel/AP setup for DFS channel
1134 * 0 - channel/AP setup will be continued after CAC
1135 * -1 - hit critical error
1137 int hostapd_handle_dfs_offload(struct hostapd_iface
*iface
)
1139 wpa_printf(MSG_DEBUG
, "%s: iface->cac_started: %d",
1140 __func__
, iface
->cac_started
);
1143 * If DFS has already been started, then we are being called from a
1144 * callback to continue AP/channel setup. Reset the CAC start flag and
1147 if (iface
->cac_started
) {
1148 wpa_printf(MSG_DEBUG
, "%s: iface->cac_started: %d",
1149 __func__
, iface
->cac_started
);
1150 iface
->cac_started
= 0;
1154 if (ieee80211_is_dfs(iface
->freq
, iface
->hw_features
,
1155 iface
->num_hw_features
)) {
1156 wpa_printf(MSG_DEBUG
, "%s: freq %d MHz requires DFS",
1157 __func__
, iface
->freq
);
1161 wpa_printf(MSG_DEBUG
,
1162 "%s: freq %d MHz does not require DFS. Continue channel/AP setup",
1163 __func__
, iface
->freq
);