2b8b90fcce6592eb87d0b50497a83ce061cff0ec
[mech_eap.git] / src / ap / dfs.c
1 /*
2  * DFS - Dynamic Frequency Selection
3  * Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi>
4  * Copyright (c) 2013, Qualcomm Atheros, Inc.
5  *
6  * This software may be distributed under the terms of the BSD license.
7  * See README for more details.
8  */
9
10 #include "utils/includes.h"
11
12 #include "utils/common.h"
13 #include "common/ieee802_11_defs.h"
14 #include "common/wpa_ctrl.h"
15 #include "hostapd.h"
16 #include "ap_drv_ops.h"
17 #include "drivers/driver.h"
18 #include "dfs.h"
19
20
21 static int dfs_get_used_n_chans(struct hostapd_iface *iface)
22 {
23         int n_chans = 1;
24
25         if (iface->conf->ieee80211n && iface->conf->secondary_channel)
26                 n_chans = 2;
27
28         if (iface->conf->ieee80211ac) {
29                 switch (iface->conf->vht_oper_chwidth) {
30                 case VHT_CHANWIDTH_USE_HT:
31                         break;
32                 case VHT_CHANWIDTH_80MHZ:
33                         n_chans = 4;
34                         break;
35                 case VHT_CHANWIDTH_160MHZ:
36                         n_chans = 8;
37                         break;
38                 default:
39                         break;
40                 }
41         }
42
43         return n_chans;
44 }
45
46
47 static int dfs_channel_available(struct hostapd_channel_data *chan,
48                                  int skip_radar)
49 {
50         /*
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
53          * channel for CSA.
54          */
55         if (skip_radar && chan->flag & HOSTAPD_CHAN_RADAR)
56                 return 0;
57
58         if (chan->flag & HOSTAPD_CHAN_DISABLED)
59                 return 0;
60         if ((chan->flag & HOSTAPD_CHAN_RADAR) &&
61             ((chan->flag & HOSTAPD_CHAN_DFS_MASK) ==
62              HOSTAPD_CHAN_DFS_UNAVAILABLE))
63                 return 0;
64         return 1;
65 }
66
67
68 static int dfs_is_chan_allowed(struct hostapd_channel_data *chan, int n_chans)
69 {
70         /*
71          * The tables contain first valid channel number based on channel width.
72          * We will also choose this first channel as the control one.
73          */
74         int allowed_40[] = { 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157,
75                              184, 192 };
76         /*
77          * VHT80, valid channels based on center frequency:
78          * 42, 58, 106, 122, 138, 155
79          */
80         int allowed_80[] = { 36, 52, 100, 116, 132, 149 };
81         /*
82          * VHT160 valid channels based on center frequency:
83          * 50, 114
84          */
85         int allowed_160[] = { 36, 100 };
86         int *allowed = allowed_40;
87         unsigned int i, allowed_no = 0;
88
89         switch (n_chans) {
90         case 2:
91                 allowed = allowed_40;
92                 allowed_no = ARRAY_SIZE(allowed_40);
93                 break;
94         case 4:
95                 allowed = allowed_80;
96                 allowed_no = ARRAY_SIZE(allowed_80);
97                 break;
98         case 8:
99                 allowed = allowed_160;
100                 allowed_no = ARRAY_SIZE(allowed_160);
101                 break;
102         default:
103                 wpa_printf(MSG_DEBUG, "Unknown width for %d channels", n_chans);
104                 break;
105         }
106
107         for (i = 0; i < allowed_no; i++) {
108                 if (chan->chan == allowed[i])
109                         return 1;
110         }
111
112         return 0;
113 }
114
115
116 static int dfs_chan_range_available(struct hostapd_hw_modes *mode,
117                                     int first_chan_idx, int num_chans,
118                                     int skip_radar)
119 {
120         struct hostapd_channel_data *first_chan, *chan;
121         int i;
122
123         if (first_chan_idx + num_chans >= mode->num_channels)
124                 return 0;
125
126         first_chan = &mode->channels[first_chan_idx];
127
128         for (i = 0; i < num_chans; i++) {
129                 chan = &mode->channels[first_chan_idx + i];
130
131                 if (first_chan->freq + i * 20 != chan->freq)
132                         return 0;
133
134                 if (!dfs_channel_available(chan, skip_radar))
135                         return 0;
136         }
137
138         return 1;
139 }
140
141
142 /*
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
148  */
149 static int dfs_find_channel(struct hostapd_iface *iface,
150                             struct hostapd_channel_data **ret_chan,
151                             int idx, int skip_radar)
152 {
153         struct hostapd_hw_modes *mode;
154         struct hostapd_channel_data *chan;
155         int i, channel_idx = 0, n_chans;
156
157         mode = iface->current_mode;
158         n_chans = dfs_get_used_n_chans(iface);
159
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];
163
164                 /* Skip HT40/VHT incompatible channels */
165                 if (iface->conf->ieee80211n &&
166                     iface->conf->secondary_channel &&
167                     !dfs_is_chan_allowed(chan, n_chans))
168                         continue;
169
170                 /* Skip incompatible chandefs */
171                 if (!dfs_chan_range_available(mode, i, n_chans, skip_radar))
172                         continue;
173
174                 if (ret_chan && idx == channel_idx) {
175                         wpa_printf(MSG_DEBUG, "Selected ch. #%d", chan->chan);
176                         *ret_chan = chan;
177                         return idx;
178                 }
179                 wpa_printf(MSG_DEBUG, "Adding channel: %d", chan->chan);
180                 channel_idx++;
181         }
182         return channel_idx;
183 }
184
185
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)
191 {
192         if (!iface->conf->ieee80211ac)
193                 return;
194
195         if (!chan)
196                 return;
197
198         *vht_oper_centr_freq_seg1_idx = 0;
199
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;
206                 else
207                         *vht_oper_centr_freq_seg0_idx = chan->chan;
208                 break;
209         case VHT_CHANWIDTH_80MHZ:
210                 *vht_oper_centr_freq_seg0_idx = chan->chan + 6;
211                 break;
212         case VHT_CHANWIDTH_160MHZ:
213                 *vht_oper_centr_freq_seg0_idx = chan->chan + 14;
214                 break;
215         default:
216                 wpa_printf(MSG_INFO, "DFS only VHT20/40/80/160 is supported now");
217                 *vht_oper_centr_freq_seg0_idx = 0;
218                 break;
219         }
220
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);
224 }
225
226
227 /* Return start channel idx we will use for mode->channels[idx] */
228 static int dfs_get_start_chan_idx(struct hostapd_iface *iface)
229 {
230         struct hostapd_hw_modes *mode;
231         struct hostapd_channel_data *chan;
232         int channel_no = iface->conf->channel;
233         int res = -1, i;
234
235         /* HT40- */
236         if (iface->conf->ieee80211n && iface->conf->secondary_channel == -1)
237                 channel_no -= 4;
238
239         /* VHT */
240         if (iface->conf->ieee80211ac) {
241                 switch (iface->conf->vht_oper_chwidth) {
242                 case VHT_CHANWIDTH_USE_HT:
243                         break;
244                 case VHT_CHANWIDTH_80MHZ:
245                         channel_no =
246                                 iface->conf->vht_oper_centr_freq_seg0_idx - 6;
247                         break;
248                 case VHT_CHANWIDTH_160MHZ:
249                         channel_no =
250                                 iface->conf->vht_oper_centr_freq_seg0_idx - 14;
251                         break;
252                 default:
253                         wpa_printf(MSG_INFO,
254                                    "DFS only VHT20/40/80/160 is supported now");
255                         channel_no = -1;
256                         break;
257                 }
258         }
259
260         /* Get idx */
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) {
265                         res = i;
266                         break;
267                 }
268         }
269
270         if (res == -1)
271                 wpa_printf(MSG_DEBUG, "DFS chan_idx seems wrong: -1");
272
273         return res;
274 }
275
276
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)
280 {
281         struct hostapd_channel_data *channel;
282         struct hostapd_hw_modes *mode;
283         int i, res = 0;
284
285         mode = iface->current_mode;
286
287         for (i = 0; i < n_chans; i++) {
288                 channel = &mode->channels[start_chan_idx + i];
289                 if (channel->flag & HOSTAPD_CHAN_RADAR)
290                         res++;
291         }
292
293         return res;
294 }
295
296
297 /* All channels available */
298 static int dfs_check_chans_available(struct hostapd_iface *iface,
299                                      int start_chan_idx, int n_chans)
300 {
301         struct hostapd_channel_data *channel;
302         struct hostapd_hw_modes *mode;
303         int i;
304
305         mode = iface->current_mode;
306
307         for (i = 0; i < n_chans; i++) {
308                 channel = &mode->channels[start_chan_idx + i];
309
310                 if (channel->flag & HOSTAPD_CHAN_DISABLED)
311                         break;
312
313                 if (!(channel->flag & HOSTAPD_CHAN_RADAR))
314                         continue;
315
316                 if ((channel->flag & HOSTAPD_CHAN_DFS_MASK) !=
317                     HOSTAPD_CHAN_DFS_AVAILABLE)
318                         break;
319         }
320
321         return i == n_chans;
322 }
323
324
325 /* At least one channel unavailable */
326 static int dfs_check_chans_unavailable(struct hostapd_iface *iface,
327                                        int start_chan_idx,
328                                        int n_chans)
329 {
330         struct hostapd_channel_data *channel;
331         struct hostapd_hw_modes *mode;
332         int i, res = 0;
333
334         mode = iface->current_mode;
335
336         for (i = 0; i < n_chans; i++) {
337                 channel = &mode->channels[start_chan_idx + i];
338                 if (channel->flag & HOSTAPD_CHAN_DISABLED)
339                         res++;
340                 if ((channel->flag & HOSTAPD_CHAN_DFS_MASK) ==
341                     HOSTAPD_CHAN_DFS_UNAVAILABLE)
342                         res++;
343         }
344
345         return res;
346 }
347
348
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,
354                       int skip_radar)
355 {
356         struct hostapd_hw_modes *mode;
357         struct hostapd_channel_data *chan = NULL;
358         int num_available_chandefs;
359         int chan_idx;
360         u32 _rand;
361
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;
366
367         if (iface->current_mode == NULL)
368                 return NULL;
369
370         mode = iface->current_mode;
371         if (mode->mode != HOSTAPD_MODE_IEEE80211A)
372                 return NULL;
373
374         /* Get the count first */
375         num_available_chandefs = dfs_find_channel(iface, NULL, 0, skip_radar);
376         if (num_available_chandefs == 0)
377                 return NULL;
378
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);
382
383         /* dfs_find_channel() calculations assume HT40+ */
384         if (iface->conf->secondary_channel)
385                 *secondary_channel = 1;
386         else
387                 *secondary_channel = 0;
388
389         dfs_adjust_vht_center_freq(iface, chan,
390                                    *secondary_channel,
391                                    vht_oper_centr_freq_seg0_idx,
392                                    vht_oper_centr_freq_seg1_idx);
393
394         return chan;
395 }
396
397
398 static int set_dfs_state_freq(struct hostapd_iface *iface, int freq, u32 state)
399 {
400         struct hostapd_hw_modes *mode;
401         struct hostapd_channel_data *chan = NULL;
402         int i;
403
404         mode = iface->current_mode;
405         if (mode == NULL)
406                 return 0;
407
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;
414                                 chan->flag |= state;
415                                 return 1; /* Channel found */
416                         }
417                 }
418         }
419         wpa_printf(MSG_WARNING, "Can't set DFS state for freq %d MHz", freq);
420         return 0;
421 }
422
423
424 static int set_dfs_state(struct hostapd_iface *iface, int freq, int ht_enabled,
425                          int chan_offset, int chan_width, int cf1,
426                          int cf2, u32 state)
427 {
428         int n_chans = 1, i;
429         struct hostapd_hw_modes *mode;
430         int frequency = freq;
431         int ret = 0;
432
433         mode = iface->current_mode;
434         if (mode == NULL)
435                 return 0;
436
437         if (mode->mode != HOSTAPD_MODE_IEEE80211A) {
438                 wpa_printf(MSG_WARNING, "current_mode != IEEE80211A");
439                 return 0;
440         }
441
442         /* Seems cf1 and chan_width is enough here */
443         switch (chan_width) {
444         case CHAN_WIDTH_20_NOHT:
445         case CHAN_WIDTH_20:
446                 n_chans = 1;
447                 if (frequency == 0)
448                         frequency = cf1;
449                 break;
450         case CHAN_WIDTH_40:
451                 n_chans = 2;
452                 frequency = cf1 - 10;
453                 break;
454         case CHAN_WIDTH_80:
455                 n_chans = 4;
456                 frequency = cf1 - 30;
457                 break;
458         case CHAN_WIDTH_160:
459                 n_chans = 8;
460                 frequency = cf1 - 70;
461                 break;
462         default:
463                 wpa_printf(MSG_INFO, "DFS chan_width %d not supported",
464                            chan_width);
465                 break;
466         }
467
468         wpa_printf(MSG_DEBUG, "DFS freq: %dMHz, n_chans: %d", frequency,
469                    n_chans);
470         for (i = 0; i < n_chans; i++) {
471                 ret += set_dfs_state_freq(iface, frequency, state);
472                 frequency = frequency + 20;
473         }
474
475         return ret;
476 }
477
478
479 static int dfs_are_channels_overlapped(struct hostapd_iface *iface, int freq,
480                                        int chan_width, int cf1, int cf2)
481 {
482         int start_chan_idx;
483         struct hostapd_hw_modes *mode;
484         struct hostapd_channel_data *chan;
485         int n_chans, i, j, frequency = freq, radar_n_chans = 1;
486         u8 radar_chan;
487         int res = 0;
488
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);
493
494         /* Check we are on DFS channel(s) */
495         if (!dfs_check_chans_radar(iface, start_chan_idx, n_chans))
496                 return 0;
497
498         /* Reported via radar event */
499         switch (chan_width) {
500         case CHAN_WIDTH_20_NOHT:
501         case CHAN_WIDTH_20:
502                 radar_n_chans = 1;
503                 if (frequency == 0)
504                         frequency = cf1;
505                 break;
506         case CHAN_WIDTH_40:
507                 radar_n_chans = 2;
508                 frequency = cf1 - 10;
509                 break;
510         case CHAN_WIDTH_80:
511                 radar_n_chans = 4;
512                 frequency = cf1 - 30;
513                 break;
514         case CHAN_WIDTH_160:
515                 radar_n_chans = 8;
516                 frequency = cf1 - 70;
517                 break;
518         default:
519                 wpa_printf(MSG_INFO, "DFS chan_width %d not supported",
520                            chan_width);
521                 break;
522         }
523
524         ieee80211_freq_to_chan(frequency, &radar_chan);
525
526         for (i = 0; i < n_chans; i++) {
527                 chan = &mode->channels[start_chan_idx + i];
528                 if (!(chan->flag & HOSTAPD_CHAN_RADAR))
529                         continue;
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)
534                                 res++;
535                 }
536         }
537
538         wpa_printf(MSG_DEBUG, "overlapped: %d", res);
539
540         return res;
541 }
542
543
544 /*
545  * Main DFS handler
546  * 1 - continue channel/ap setup
547  * 0 - channel/ap setup will be continued after CAC
548  * -1 - hit critical error
549  */
550 int hostapd_handle_dfs(struct hostapd_iface *iface)
551 {
552         struct hostapd_channel_data *channel;
553         int res, n_chans, start_chan_idx;
554         int skip_radar = 0;
555
556         iface->cac_started = 0;
557
558         do {
559                 /* Get start (first) channel for current configuration */
560                 start_chan_idx = dfs_get_start_chan_idx(iface);
561                 if (start_chan_idx == -1)
562                         return -1;
563
564                 /* Get number of used channels, depend on width */
565                 n_chans = dfs_get_used_n_chans(iface);
566
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",
571                            res);
572                 if (!res)
573                         return 1;
574
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",
579                            res ? "yes" : "no");
580                 if (res)
581                         return 1;
582
583                 /* Check if any of configured channels is unavailable */
584                 res = dfs_check_chans_unavailable(iface, start_chan_idx,
585                                                   n_chans);
586                 wpa_printf(MSG_DEBUG, "DFS %d chans unavailable - choose other channel: %s",
587                            res, res ? "yes": "no");
588                 if (res) {
589                         int sec = 0;
590                         u8 cf1 = 0, cf2 = 0;
591
592                         channel = dfs_get_valid_channel(iface, &sec, &cf1, &cf2,
593                                                         skip_radar);
594                         if (!channel) {
595                                 wpa_printf(MSG_ERROR, "could not get valid channel");
596                                 return -1;
597                         }
598
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;
604                 }
605         } while (res);
606
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",
612                 iface->freq,
613                 iface->conf->channel, iface->conf->secondary_channel);
614         if (hostapd_start_dfs_cac(iface, iface->conf->hw_mode,
615                                   iface->freq,
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");
624                 return -1;
625         }
626
627         return 0;
628 }
629
630
631 int hostapd_dfs_complete_cac(struct hostapd_iface *iface, int success, int freq,
632                              int ht_enabled, int chan_offset, int chan_width,
633                              int cf1, int cf2)
634 {
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);
638
639         if (success) {
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);
646         }
647
648         return 0;
649 }
650
651
652 static int hostapd_dfs_start_channel_switch_cac(struct hostapd_iface *iface)
653 {
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;
658         int skip_radar = 0;
659         int err = 1;
660
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,
666                                         skip_radar);
667
668         if (!channel) {
669                 wpa_printf(MSG_ERROR, "No valid channel available");
670                 hostapd_setup_interface_complete(iface, err);
671                 return err;
672         }
673
674         wpa_printf(MSG_DEBUG, "DFS will switch to a new channel %d",
675                    channel->chan);
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);
679
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;
687         err = 0;
688
689         hostapd_setup_interface_complete(iface, err);
690         return err;
691 }
692
693
694 static int hostapd_dfs_start_channel_switch(struct hostapd_iface *iface)
695 {
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;
700         int skip_radar = 1;
701         struct csa_settings csa_settings;
702         struct hostapd_data *hapd = iface->bss[0];
703         int err = 1;
704
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");
708
709         /* Check if CSA in progress */
710         if (iface->csa_in_progress)
711                 return 0;
712
713         /* Check if active CAC */
714         if (iface->cac_started)
715                 return hostapd_dfs_start_channel_switch_cac(iface);
716
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,
721                                         skip_radar);
722
723         if (!channel) {
724                 /* FIXME: Wait for channel(s) to become available */
725                 hostapd_disable_iface(iface);
726                 return err;
727         }
728
729         wpa_printf(MSG_DEBUG, "DFS will switch to a new channel %d",
730                    channel->chan);
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);
734
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,
741                                       channel->freq,
742                                       channel->chan,
743                                       iface->conf->ieee80211n,
744                                       iface->conf->ieee80211ac,
745                                       secondary_channel,
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);
750
751         if (err) {
752                 wpa_printf(MSG_ERROR, "DFS failed to calculate CSA freq params");
753                 hostapd_disable_iface(iface);
754                 return err;
755         }
756
757         err = hostapd_switch_channel(hapd, &csa_settings);
758         if (err) {
759                 wpa_printf(MSG_WARNING, "DFS failed to schedule CSA (%d) - trying fallback",
760                            err);
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;
768
769                 hostapd_disable_iface(iface);
770                 hostapd_enable_iface(iface);
771                 return 0;
772         }
773
774         /* Channel configuration will be updated once CSA completes and
775          * ch_switch_notify event is received */
776
777         wpa_printf(MSG_DEBUG, "DFS waiting channel switch event");
778         return 0;
779 }
780
781
782 int hostapd_dfs_radar_detected(struct hostapd_iface *iface, int freq,
783                                int ht_enabled, int chan_offset, int chan_width,
784                                int cf1, int cf2)
785 {
786         int res;
787
788         if (!iface->conf->ieee80211h)
789                 return 0;
790
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);
794
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);
799
800         /* Skip if reported radar event not overlapped our channels */
801         res = dfs_are_channels_overlapped(iface, freq, chan_width, cf1, cf2);
802         if (!res)
803                 return 0;
804
805         /* radar detected while operating, switch the channel. */
806         res = hostapd_dfs_start_channel_switch(iface);
807
808         return res;
809 }
810
811
812 int hostapd_dfs_nop_finished(struct hostapd_iface *iface, int freq,
813                              int ht_enabled, int chan_offset, int chan_width,
814                              int cf1, int cf2)
815 {
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);
822         return 0;
823 }
824
825
826 int hostapd_is_dfs_required(struct hostapd_iface *iface)
827 {
828         int n_chans, start_chan_idx;
829
830         if (!iface->current_mode)
831                 return -1;
832
833         /* Get start (first) channel for current configuration */
834         start_chan_idx = dfs_get_start_chan_idx(iface);
835         if (start_chan_idx == -1)
836                 return -1;
837
838         /* Get number of used channels, depend on width */
839         n_chans = dfs_get_used_n_chans(iface);
840
841         /* Check if any of configured channels require DFS */
842         return dfs_check_chans_radar(iface, start_chan_idx, n_chans);
843 }