2 * DFS - Dynamic Frequency Selection
3 * Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi>
4 * Copyright (c) 2013, 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/wpa_ctrl.h"
16 #include "ap_drv_ops.h"
17 #include "drivers/driver.h"
21 static int dfs_get_used_n_chans(struct hostapd_iface *iface)
25 if (iface->conf->ieee80211n && iface->conf->secondary_channel)
28 if (iface->conf->ieee80211ac) {
29 switch (iface->conf->vht_oper_chwidth) {
30 case VHT_CHANWIDTH_USE_HT:
32 case VHT_CHANWIDTH_80MHZ:
35 case VHT_CHANWIDTH_160MHZ:
47 static int dfs_channel_available(struct hostapd_channel_data *chan,
51 * When radar detection happens, CSA is performed. However, there's no
52 * time for CAC, so radar channels must be skipped when finding a new
55 if (skip_radar && chan->flag & HOSTAPD_CHAN_RADAR)
58 if (chan->flag & HOSTAPD_CHAN_DISABLED)
60 if ((chan->flag & HOSTAPD_CHAN_RADAR) &&
61 ((chan->flag & HOSTAPD_CHAN_DFS_MASK) ==
62 HOSTAPD_CHAN_DFS_UNAVAILABLE))
68 static int dfs_is_chan_allowed(struct hostapd_channel_data *chan, int n_chans)
71 * The tables contain first valid channel number based on channel width.
72 * We will also choose this first channel as the control one.
74 int allowed_40[] = { 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157,
77 * VHT80, valid channels based on center frequency:
78 * 42, 58, 106, 122, 138, 155
80 int allowed_80[] = { 36, 52, 100, 116, 132, 149 };
82 * VHT160 valid channels based on center frequency:
85 int allowed_160[] = { 36, 100 };
86 int *allowed = allowed_40;
87 unsigned int i, allowed_no = 0;
92 allowed_no = ARRAY_SIZE(allowed_40);
96 allowed_no = ARRAY_SIZE(allowed_80);
99 allowed = allowed_160;
100 allowed_no = ARRAY_SIZE(allowed_160);
103 wpa_printf(MSG_DEBUG, "Unknown width for %d channels", n_chans);
107 for (i = 0; i < allowed_no; i++) {
108 if (chan->chan == allowed[i])
116 static int dfs_chan_range_available(struct hostapd_hw_modes *mode,
117 int first_chan_idx, int num_chans,
120 struct hostapd_channel_data *first_chan, *chan;
123 if (first_chan_idx + num_chans >= mode->num_channels)
126 first_chan = &mode->channels[first_chan_idx];
128 for (i = 0; i < num_chans; i++) {
129 chan = &mode->channels[first_chan_idx + i];
131 if (first_chan->freq + i * 20 != chan->freq)
134 if (!dfs_channel_available(chan, skip_radar))
143 * The function assumes HT40+ operation.
144 * Make sure to adjust the following variables after calling this:
145 * - hapd->secondary_channel
146 * - hapd->vht_oper_centr_freq_seg0_idx
147 * - hapd->vht_oper_centr_freq_seg1_idx
149 static int dfs_find_channel(struct hostapd_iface *iface,
150 struct hostapd_channel_data **ret_chan,
151 int idx, int skip_radar)
153 struct hostapd_hw_modes *mode;
154 struct hostapd_channel_data *chan;
155 int i, channel_idx = 0, n_chans;
157 mode = iface->current_mode;
158 n_chans = dfs_get_used_n_chans(iface);
160 wpa_printf(MSG_DEBUG, "DFS new chan checking %d channels", n_chans);
161 for (i = 0; i < mode->num_channels; i++) {
162 chan = &mode->channels[i];
164 /* Skip HT40/VHT incompatible channels */
165 if (iface->conf->ieee80211n &&
166 iface->conf->secondary_channel &&
167 !dfs_is_chan_allowed(chan, n_chans))
170 /* Skip incompatible chandefs */
171 if (!dfs_chan_range_available(mode, i, n_chans, skip_radar))
174 if (ret_chan && idx == channel_idx) {
175 wpa_printf(MSG_DEBUG, "Selected ch. #%d", chan->chan);
179 wpa_printf(MSG_DEBUG, "Adding channel: %d", chan->chan);
186 static void dfs_adjust_vht_center_freq(struct hostapd_iface *iface,
187 struct hostapd_channel_data *chan,
188 int secondary_channel,
189 u8 *vht_oper_centr_freq_seg0_idx,
190 u8 *vht_oper_centr_freq_seg1_idx)
192 if (!iface->conf->ieee80211ac)
198 *vht_oper_centr_freq_seg1_idx = 0;
200 switch (iface->conf->vht_oper_chwidth) {
201 case VHT_CHANWIDTH_USE_HT:
202 if (secondary_channel == 1)
203 *vht_oper_centr_freq_seg0_idx = chan->chan + 2;
204 else if (secondary_channel == -1)
205 *vht_oper_centr_freq_seg0_idx = chan->chan - 2;
207 *vht_oper_centr_freq_seg0_idx = chan->chan;
209 case VHT_CHANWIDTH_80MHZ:
210 *vht_oper_centr_freq_seg0_idx = chan->chan + 6;
212 case VHT_CHANWIDTH_160MHZ:
213 *vht_oper_centr_freq_seg0_idx = chan->chan + 14;
216 wpa_printf(MSG_INFO, "DFS only VHT20/40/80/160 is supported now");
217 *vht_oper_centr_freq_seg0_idx = 0;
221 wpa_printf(MSG_DEBUG, "DFS adjusting VHT center frequency: %d, %d",
222 *vht_oper_centr_freq_seg0_idx,
223 *vht_oper_centr_freq_seg1_idx);
227 /* Return start channel idx we will use for mode->channels[idx] */
228 static int dfs_get_start_chan_idx(struct hostapd_iface *iface)
230 struct hostapd_hw_modes *mode;
231 struct hostapd_channel_data *chan;
232 int channel_no = iface->conf->channel;
236 if (iface->conf->ieee80211n && iface->conf->secondary_channel == -1)
240 if (iface->conf->ieee80211ac) {
241 switch (iface->conf->vht_oper_chwidth) {
242 case VHT_CHANWIDTH_USE_HT:
244 case VHT_CHANWIDTH_80MHZ:
246 iface->conf->vht_oper_centr_freq_seg0_idx - 6;
248 case VHT_CHANWIDTH_160MHZ:
250 iface->conf->vht_oper_centr_freq_seg0_idx - 14;
254 "DFS only VHT20/40/80/160 is supported now");
261 mode = iface->current_mode;
262 for (i = 0; i < mode->num_channels; i++) {
263 chan = &mode->channels[i];
264 if (chan->chan == channel_no) {
271 wpa_printf(MSG_DEBUG, "DFS chan_idx seems wrong: -1");
277 /* At least one channel have radar flag */
278 static int dfs_check_chans_radar(struct hostapd_iface *iface,
279 int start_chan_idx, int n_chans)
281 struct hostapd_channel_data *channel;
282 struct hostapd_hw_modes *mode;
285 mode = iface->current_mode;
287 for (i = 0; i < n_chans; i++) {
288 channel = &mode->channels[start_chan_idx + i];
289 if (channel->flag & HOSTAPD_CHAN_RADAR)
297 /* All channels available */
298 static int dfs_check_chans_available(struct hostapd_iface *iface,
299 int start_chan_idx, int n_chans)
301 struct hostapd_channel_data *channel;
302 struct hostapd_hw_modes *mode;
305 mode = iface->current_mode;
307 for (i = 0; i < n_chans; i++) {
308 channel = &mode->channels[start_chan_idx + i];
310 if (channel->flag & HOSTAPD_CHAN_DISABLED)
313 if (!(channel->flag & HOSTAPD_CHAN_RADAR))
316 if ((channel->flag & HOSTAPD_CHAN_DFS_MASK) !=
317 HOSTAPD_CHAN_DFS_AVAILABLE)
325 /* At least one channel unavailable */
326 static int dfs_check_chans_unavailable(struct hostapd_iface *iface,
330 struct hostapd_channel_data *channel;
331 struct hostapd_hw_modes *mode;
334 mode = iface->current_mode;
336 for (i = 0; i < n_chans; i++) {
337 channel = &mode->channels[start_chan_idx + i];
338 if (channel->flag & HOSTAPD_CHAN_DISABLED)
340 if ((channel->flag & HOSTAPD_CHAN_DFS_MASK) ==
341 HOSTAPD_CHAN_DFS_UNAVAILABLE)
349 static struct hostapd_channel_data *
350 dfs_get_valid_channel(struct hostapd_iface *iface,
351 int *secondary_channel,
352 u8 *vht_oper_centr_freq_seg0_idx,
353 u8 *vht_oper_centr_freq_seg1_idx,
356 struct hostapd_hw_modes *mode;
357 struct hostapd_channel_data *chan = NULL;
358 int num_available_chandefs;
362 wpa_printf(MSG_DEBUG, "DFS: Selecting random channel");
363 *secondary_channel = 0;
364 *vht_oper_centr_freq_seg0_idx = 0;
365 *vht_oper_centr_freq_seg1_idx = 0;
367 if (iface->current_mode == NULL)
370 mode = iface->current_mode;
371 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
374 /* Get the count first */
375 num_available_chandefs = dfs_find_channel(iface, NULL, 0, skip_radar);
376 if (num_available_chandefs == 0)
379 os_get_random((u8 *) &_rand, sizeof(_rand));
380 chan_idx = _rand % num_available_chandefs;
381 dfs_find_channel(iface, &chan, chan_idx, skip_radar);
383 /* dfs_find_channel() calculations assume HT40+ */
384 if (iface->conf->secondary_channel)
385 *secondary_channel = 1;
387 *secondary_channel = 0;
389 dfs_adjust_vht_center_freq(iface, chan,
391 vht_oper_centr_freq_seg0_idx,
392 vht_oper_centr_freq_seg1_idx);
398 static int set_dfs_state_freq(struct hostapd_iface *iface, int freq, u32 state)
400 struct hostapd_hw_modes *mode;
401 struct hostapd_channel_data *chan = NULL;
404 mode = iface->current_mode;
408 wpa_printf(MSG_DEBUG, "set_dfs_state 0x%X for %d MHz", state, freq);
409 for (i = 0; i < iface->current_mode->num_channels; i++) {
410 chan = &iface->current_mode->channels[i];
411 if (chan->freq == freq) {
412 if (chan->flag & HOSTAPD_CHAN_RADAR) {
413 chan->flag &= ~HOSTAPD_CHAN_DFS_MASK;
415 return 1; /* Channel found */
419 wpa_printf(MSG_WARNING, "Can't set DFS state for freq %d MHz", freq);
424 static int set_dfs_state(struct hostapd_iface *iface, int freq, int ht_enabled,
425 int chan_offset, int chan_width, int cf1,
429 struct hostapd_hw_modes *mode;
430 int frequency = freq;
433 mode = iface->current_mode;
437 if (mode->mode != HOSTAPD_MODE_IEEE80211A) {
438 wpa_printf(MSG_WARNING, "current_mode != IEEE80211A");
442 /* Seems cf1 and chan_width is enough here */
443 switch (chan_width) {
444 case CHAN_WIDTH_20_NOHT:
452 frequency = cf1 - 10;
456 frequency = cf1 - 30;
460 frequency = cf1 - 70;
463 wpa_printf(MSG_INFO, "DFS chan_width %d not supported",
468 wpa_printf(MSG_DEBUG, "DFS freq: %dMHz, n_chans: %d", frequency,
470 for (i = 0; i < n_chans; i++) {
471 ret += set_dfs_state_freq(iface, frequency, state);
472 frequency = frequency + 20;
479 static int dfs_are_channels_overlapped(struct hostapd_iface *iface, int freq,
480 int chan_width, int cf1, int cf2)
483 struct hostapd_hw_modes *mode;
484 struct hostapd_channel_data *chan;
485 int n_chans, i, j, frequency = freq, radar_n_chans = 1;
489 /* Our configuration */
490 mode = iface->current_mode;
491 start_chan_idx = dfs_get_start_chan_idx(iface);
492 n_chans = dfs_get_used_n_chans(iface);
494 /* Check we are on DFS channel(s) */
495 if (!dfs_check_chans_radar(iface, start_chan_idx, n_chans))
498 /* Reported via radar event */
499 switch (chan_width) {
500 case CHAN_WIDTH_20_NOHT:
508 frequency = cf1 - 10;
512 frequency = cf1 - 30;
516 frequency = cf1 - 70;
519 wpa_printf(MSG_INFO, "DFS chan_width %d not supported",
524 ieee80211_freq_to_chan(frequency, &radar_chan);
526 for (i = 0; i < n_chans; i++) {
527 chan = &mode->channels[start_chan_idx + i];
528 if (!(chan->flag & HOSTAPD_CHAN_RADAR))
530 for (j = 0; j < radar_n_chans; j++) {
531 wpa_printf(MSG_DEBUG, "checking our: %d, radar: %d",
532 chan->chan, radar_chan + j * 4);
533 if (chan->chan == radar_chan + j * 4)
538 wpa_printf(MSG_DEBUG, "overlapped: %d", res);
546 * 1 - continue channel/ap setup
547 * 0 - channel/ap setup will be continued after CAC
548 * -1 - hit critical error
550 int hostapd_handle_dfs(struct hostapd_iface *iface)
552 struct hostapd_channel_data *channel;
553 int res, n_chans, start_chan_idx;
556 iface->cac_started = 0;
559 /* Get start (first) channel for current configuration */
560 start_chan_idx = dfs_get_start_chan_idx(iface);
561 if (start_chan_idx == -1)
564 /* Get number of used channels, depend on width */
565 n_chans = dfs_get_used_n_chans(iface);
567 /* Check if any of configured channels require DFS */
568 res = dfs_check_chans_radar(iface, start_chan_idx, n_chans);
569 wpa_printf(MSG_DEBUG,
570 "DFS %d channels required radar detection",
575 /* Check if all channels are DFS available */
576 res = dfs_check_chans_available(iface, start_chan_idx, n_chans);
577 wpa_printf(MSG_DEBUG,
578 "DFS all channels available, (SKIP CAC): %s",
583 /* Check if any of configured channels is unavailable */
584 res = dfs_check_chans_unavailable(iface, start_chan_idx,
586 wpa_printf(MSG_DEBUG, "DFS %d chans unavailable - choose other channel: %s",
587 res, res ? "yes": "no");
592 channel = dfs_get_valid_channel(iface, &sec, &cf1, &cf2,
595 wpa_printf(MSG_ERROR, "could not get valid channel");
599 iface->freq = channel->freq;
600 iface->conf->channel = channel->chan;
601 iface->conf->secondary_channel = sec;
602 iface->conf->vht_oper_centr_freq_seg0_idx = cf1;
603 iface->conf->vht_oper_centr_freq_seg1_idx = cf2;
607 /* Finally start CAC */
608 hostapd_set_state(iface, HAPD_IFACE_DFS);
609 wpa_printf(MSG_DEBUG, "DFS start CAC on %d MHz", iface->freq);
610 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_START
611 "freq=%d chan=%d sec_chan=%d",
613 iface->conf->channel, iface->conf->secondary_channel);
614 if (hostapd_start_dfs_cac(iface, iface->conf->hw_mode,
616 iface->conf->channel,
617 iface->conf->ieee80211n,
618 iface->conf->ieee80211ac,
619 iface->conf->secondary_channel,
620 iface->conf->vht_oper_chwidth,
621 iface->conf->vht_oper_centr_freq_seg0_idx,
622 iface->conf->vht_oper_centr_freq_seg1_idx)) {
623 wpa_printf(MSG_DEBUG, "DFS start_dfs_cac() failed");
631 int hostapd_dfs_complete_cac(struct hostapd_iface *iface, int success, int freq,
632 int ht_enabled, int chan_offset, int chan_width,
635 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_COMPLETED
636 "success=%d freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
637 success, freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
640 /* Complete iface/ap configuration */
641 set_dfs_state(iface, freq, ht_enabled, chan_offset,
642 chan_width, cf1, cf2,
643 HOSTAPD_CHAN_DFS_AVAILABLE);
644 iface->cac_started = 0;
645 hostapd_setup_interface_complete(iface, 0);
652 static int hostapd_dfs_start_channel_switch_cac(struct hostapd_iface *iface)
654 struct hostapd_channel_data *channel;
655 int secondary_channel;
656 u8 vht_oper_centr_freq_seg0_idx = 0;
657 u8 vht_oper_centr_freq_seg1_idx = 0;
661 /* Radar detected during active CAC */
662 iface->cac_started = 0;
663 channel = dfs_get_valid_channel(iface, &secondary_channel,
664 &vht_oper_centr_freq_seg0_idx,
665 &vht_oper_centr_freq_seg1_idx,
669 wpa_printf(MSG_ERROR, "No valid channel available");
670 hostapd_setup_interface_complete(iface, err);
674 wpa_printf(MSG_DEBUG, "DFS will switch to a new channel %d",
676 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NEW_CHANNEL
677 "freq=%d chan=%d sec_chan=%d", channel->freq,
678 channel->chan, secondary_channel);
680 iface->freq = channel->freq;
681 iface->conf->channel = channel->chan;
682 iface->conf->secondary_channel = secondary_channel;
683 iface->conf->vht_oper_centr_freq_seg0_idx =
684 vht_oper_centr_freq_seg0_idx;
685 iface->conf->vht_oper_centr_freq_seg1_idx =
686 vht_oper_centr_freq_seg1_idx;
689 hostapd_setup_interface_complete(iface, err);
694 static int hostapd_dfs_start_channel_switch(struct hostapd_iface *iface)
696 struct hostapd_channel_data *channel;
697 int secondary_channel;
698 u8 vht_oper_centr_freq_seg0_idx;
699 u8 vht_oper_centr_freq_seg1_idx;
701 struct csa_settings csa_settings;
702 struct hostapd_data *hapd = iface->bss[0];
705 wpa_printf(MSG_DEBUG, "%s called (CAC active: %s, CSA active: %s)",
706 __func__, iface->cac_started ? "yes" : "no",
707 iface->csa_in_progress ? "yes" : "no");
709 /* Check if CSA in progress */
710 if (iface->csa_in_progress)
713 /* Check if active CAC */
714 if (iface->cac_started)
715 return hostapd_dfs_start_channel_switch_cac(iface);
717 /* Perform channel switch/CSA */
718 channel = dfs_get_valid_channel(iface, &secondary_channel,
719 &vht_oper_centr_freq_seg0_idx,
720 &vht_oper_centr_freq_seg1_idx,
724 /* FIXME: Wait for channel(s) to become available */
725 hostapd_disable_iface(iface);
729 wpa_printf(MSG_DEBUG, "DFS will switch to a new channel %d",
731 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NEW_CHANNEL
732 "freq=%d chan=%d sec_chan=%d", channel->freq,
733 channel->chan, secondary_channel);
735 /* Setup CSA request */
736 os_memset(&csa_settings, 0, sizeof(csa_settings));
737 csa_settings.cs_count = 5;
738 csa_settings.block_tx = 1;
739 err = hostapd_set_freq_params(&csa_settings.freq_params,
740 iface->conf->hw_mode,
743 iface->conf->ieee80211n,
744 iface->conf->ieee80211ac,
746 iface->conf->vht_oper_chwidth,
747 vht_oper_centr_freq_seg0_idx,
748 vht_oper_centr_freq_seg1_idx,
749 iface->current_mode->vht_capab);
752 wpa_printf(MSG_ERROR, "DFS failed to calculate CSA freq params");
753 hostapd_disable_iface(iface);
757 err = hostapd_switch_channel(hapd, &csa_settings);
759 wpa_printf(MSG_WARNING, "DFS failed to schedule CSA (%d) - trying fallback",
761 iface->freq = channel->freq;
762 iface->conf->channel = channel->chan;
763 iface->conf->secondary_channel = secondary_channel;
764 iface->conf->vht_oper_centr_freq_seg0_idx =
765 vht_oper_centr_freq_seg0_idx;
766 iface->conf->vht_oper_centr_freq_seg1_idx =
767 vht_oper_centr_freq_seg1_idx;
769 hostapd_disable_iface(iface);
770 hostapd_enable_iface(iface);
774 /* Channel configuration will be updated once CSA completes and
775 * ch_switch_notify event is received */
777 wpa_printf(MSG_DEBUG, "DFS waiting channel switch event");
782 int hostapd_dfs_radar_detected(struct hostapd_iface *iface, int freq,
783 int ht_enabled, int chan_offset, int chan_width,
788 if (!iface->conf->ieee80211h)
791 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_RADAR_DETECTED
792 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
793 freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
795 /* mark radar frequency as invalid */
796 res = set_dfs_state(iface, freq, ht_enabled, chan_offset,
797 chan_width, cf1, cf2,
798 HOSTAPD_CHAN_DFS_UNAVAILABLE);
800 /* Skip if reported radar event not overlapped our channels */
801 res = dfs_are_channels_overlapped(iface, freq, chan_width, cf1, cf2);
805 /* radar detected while operating, switch the channel. */
806 res = hostapd_dfs_start_channel_switch(iface);
812 int hostapd_dfs_nop_finished(struct hostapd_iface *iface, int freq,
813 int ht_enabled, int chan_offset, int chan_width,
816 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NOP_FINISHED
817 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
818 freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
819 /* TODO add correct implementation here */
820 set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
821 cf1, cf2, HOSTAPD_CHAN_DFS_USABLE);
826 int hostapd_is_dfs_required(struct hostapd_iface *iface)
828 int n_chans, start_chan_idx;
830 if (!iface->current_mode)
833 /* Get start (first) channel for current configuration */
834 start_chan_idx = dfs_get_start_chan_idx(iface);
835 if (start_chan_idx == -1)
838 /* Get number of used channels, depend on width */
839 n_chans = dfs_get_used_n_chans(iface);
841 /* Check if any of configured channels require DFS */
842 return dfs_check_chans_radar(iface, start_chan_idx, n_chans);