]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/ap/dfs.c
tests: Automatic channel selection with fallback to 20 MHz
[thirdparty/hostap.git] / src / ap / dfs.c
CommitLineData
e76da505
JD
1/*
2 * DFS - Dynamic Frequency Selection
3 * Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi>
7cbb5f1a 4 * Copyright (c) 2013-2017, Qualcomm Atheros, Inc.
e76da505
JD
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"
ada157f3 14#include "common/hw_features_common.h"
186c9059 15#include "common/wpa_ctrl.h"
e76da505 16#include "hostapd.h"
e76da505
JD
17#include "ap_drv_ops.h"
18#include "drivers/driver.h"
19#include "dfs.h"
20
21
4a274f48 22static int dfs_get_used_n_chans(struct hostapd_iface *iface, int *seg1)
58b73e3d
JD
23{
24 int n_chans = 1;
25
4a274f48
JM
26 *seg1 = 0;
27
dc036d9e 28 if (iface->conf->ieee80211n && iface->conf->secondary_channel)
58b73e3d
JD
29 n_chans = 2;
30
958cb348 31 if (iface->conf->ieee80211ac || iface->conf->ieee80211ax) {
c6b7ac07 32 switch (hostapd_get_oper_chwidth(iface->conf)) {
464dcfd0 33 case CHANWIDTH_USE_HT:
58b73e3d 34 break;
464dcfd0 35 case CHANWIDTH_80MHZ:
58b73e3d
JD
36 n_chans = 4;
37 break;
464dcfd0 38 case CHANWIDTH_160MHZ:
899cc14e
JD
39 n_chans = 8;
40 break;
464dcfd0 41 case CHANWIDTH_80P80MHZ:
4a274f48
JM
42 n_chans = 4;
43 *seg1 = 4;
44 break;
58b73e3d
JD
45 default:
46 break;
47 }
48 }
49
50 return n_chans;
51}
52
53
b72f949b
MK
54static int dfs_channel_available(struct hostapd_channel_data *chan,
55 int skip_radar)
58b73e3d 56{
b72f949b
MK
57 /*
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
01b99998 60 * channel for CSA, unless they are available for immediate use.
b72f949b 61 */
01b99998
SW
62 if (skip_radar && (chan->flag & HOSTAPD_CHAN_RADAR) &&
63 ((chan->flag & HOSTAPD_CHAN_DFS_MASK) !=
64 HOSTAPD_CHAN_DFS_AVAILABLE))
b72f949b
MK
65 return 0;
66
58b73e3d
JD
67 if (chan->flag & HOSTAPD_CHAN_DISABLED)
68 return 0;
69 if ((chan->flag & HOSTAPD_CHAN_RADAR) &&
70 ((chan->flag & HOSTAPD_CHAN_DFS_MASK) ==
71 HOSTAPD_CHAN_DFS_UNAVAILABLE))
72 return 0;
73 return 1;
74}
75
76
1dc17db3 77static int dfs_is_chan_allowed(struct hostapd_channel_data *chan, int n_chans)
58b73e3d 78{
1dc17db3
JD
79 /*
80 * The tables contain first valid channel number based on channel width.
81 * We will also choose this first channel as the control one.
82 */
83 int allowed_40[] = { 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157,
84 184, 192 };
85 /*
86 * VHT80, valid channels based on center frequency:
87 * 42, 58, 106, 122, 138, 155
88 */
89 int allowed_80[] = { 36, 52, 100, 116, 132, 149 };
4f1e01b8
JD
90 /*
91 * VHT160 valid channels based on center frequency:
92 * 50, 114
93 */
94 int allowed_160[] = { 36, 100 };
1dc17db3
JD
95 int *allowed = allowed_40;
96 unsigned int i, allowed_no = 0;
97
98 switch (n_chans) {
99 case 2:
100 allowed = allowed_40;
e7ecab4a 101 allowed_no = ARRAY_SIZE(allowed_40);
1dc17db3
JD
102 break;
103 case 4:
104 allowed = allowed_80;
e7ecab4a 105 allowed_no = ARRAY_SIZE(allowed_80);
1dc17db3 106 break;
4f1e01b8
JD
107 case 8:
108 allowed = allowed_160;
109 allowed_no = ARRAY_SIZE(allowed_160);
110 break;
1dc17db3
JD
111 default:
112 wpa_printf(MSG_DEBUG, "Unknown width for %d channels", n_chans);
113 break;
114 }
58b73e3d 115
1dc17db3 116 for (i = 0; i < allowed_no; i++) {
58b73e3d
JD
117 if (chan->chan == allowed[i])
118 return 1;
119 }
120
121 return 0;
122}
123
124
56ef9925
EP
125static struct hostapd_channel_data *
126dfs_get_chan_data(struct hostapd_hw_modes *mode, int freq, int first_chan_idx)
127{
128 int i;
129
130 for (i = first_chan_idx; i < mode->num_channels; i++) {
131 if (mode->channels[i].freq == freq)
132 return &mode->channels[i];
133 }
134
135 return NULL;
136}
137
138
6a398ddc 139static int dfs_chan_range_available(struct hostapd_hw_modes *mode,
b72f949b
MK
140 int first_chan_idx, int num_chans,
141 int skip_radar)
6a398ddc
MK
142{
143 struct hostapd_channel_data *first_chan, *chan;
144 int i;
59bf0f97 145 u32 bw = num_chan_to_bw(num_chans);
6a398ddc 146
8f89e57a
JM
147 if (first_chan_idx + num_chans > mode->num_channels) {
148 wpa_printf(MSG_DEBUG,
149 "DFS: some channels in range not defined");
6a398ddc 150 return 0;
8f89e57a 151 }
6a398ddc
MK
152
153 first_chan = &mode->channels[first_chan_idx];
154
59bf0f97
DL
155 /* hostapd DFS implementation assumes the first channel as primary.
156 * If it's not allowed to use the first channel as primary, decline the
157 * whole channel range. */
8f89e57a
JM
158 if (!chan_pri_allowed(first_chan)) {
159 wpa_printf(MSG_DEBUG, "DFS: primary chanenl not allowed");
59bf0f97 160 return 0;
8f89e57a 161 }
59bf0f97 162
6a398ddc 163 for (i = 0; i < num_chans; i++) {
56ef9925
EP
164 chan = dfs_get_chan_data(mode, first_chan->freq + i * 20,
165 first_chan_idx);
8f89e57a
JM
166 if (!chan) {
167 wpa_printf(MSG_DEBUG, "DFS: no channel data for %d",
168 first_chan->freq + i * 20);
6a398ddc 169 return 0;
8f89e57a 170 }
6a398ddc 171
59bf0f97
DL
172 /* HT 40 MHz secondary channel availability checked only for
173 * primary channel */
8f89e57a
JM
174 if (!chan_bw_allowed(chan, bw, 1, !i)) {
175 wpa_printf(MSG_DEBUG, "DFS: bw now allowed for %d",
176 first_chan->freq + i * 20);
59bf0f97 177 return 0;
8f89e57a 178 }
59bf0f97 179
8f89e57a
JM
180 if (!dfs_channel_available(chan, skip_radar)) {
181 wpa_printf(MSG_DEBUG, "DFS: channel not available %d",
182 first_chan->freq + i * 20);
6a398ddc 183 return 0;
8f89e57a 184 }
6a398ddc
MK
185 }
186
187 return 1;
188}
189
190
70ee1be2
SW
191static int is_in_chanlist(struct hostapd_iface *iface,
192 struct hostapd_channel_data *chan)
193{
857d9422 194 if (!iface->conf->acs_ch_list.num)
70ee1be2
SW
195 return 1;
196
857d9422 197 return freq_range_list_includes(&iface->conf->acs_ch_list, chan->chan);
70ee1be2
SW
198}
199
200
32595da6
MK
201/*
202 * The function assumes HT40+ operation.
203 * Make sure to adjust the following variables after calling this:
204 * - hapd->secondary_channel
c6b7ac07
JC
205 * - hapd->vht/he_oper_centr_freq_seg0_idx
206 * - hapd->vht/he_oper_centr_freq_seg1_idx
32595da6 207 */
dc036d9e 208static int dfs_find_channel(struct hostapd_iface *iface,
6de0e0c9 209 struct hostapd_channel_data **ret_chan,
b72f949b 210 int idx, int skip_radar)
e76da505
JD
211{
212 struct hostapd_hw_modes *mode;
6a398ddc 213 struct hostapd_channel_data *chan;
4a274f48 214 int i, channel_idx = 0, n_chans, n_chans1;
e76da505 215
dc036d9e 216 mode = iface->current_mode;
4a274f48 217 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
e76da505 218
58b73e3d 219 wpa_printf(MSG_DEBUG, "DFS new chan checking %d channels", n_chans);
e76da505
JD
220 for (i = 0; i < mode->num_channels; i++) {
221 chan = &mode->channels[i];
222
6a398ddc 223 /* Skip HT40/VHT incompatible channels */
dc036d9e
JM
224 if (iface->conf->ieee80211n &&
225 iface->conf->secondary_channel &&
59bf0f97 226 (!dfs_is_chan_allowed(chan, n_chans) ||
8f89e57a
JM
227 !(chan->allowed_bw & HOSTAPD_CHAN_WIDTH_40P))) {
228 wpa_printf(MSG_DEBUG,
229 "DFS: channel %d (%d) is incompatible",
230 chan->freq, chan->chan);
e76da505 231 continue;
8f89e57a 232 }
e76da505 233
6a398ddc 234 /* Skip incompatible chandefs */
8f89e57a
JM
235 if (!dfs_chan_range_available(mode, i, n_chans, skip_radar)) {
236 wpa_printf(MSG_DEBUG,
237 "DFS: range not available for %d (%d)",
238 chan->freq, chan->chan);
6a398ddc 239 continue;
8f89e57a 240 }
e76da505 241
8f89e57a
JM
242 if (!is_in_chanlist(iface, chan)) {
243 wpa_printf(MSG_DEBUG,
244 "DFS: channel %d (%d) not in chanlist",
245 chan->freq, chan->chan);
70ee1be2 246 continue;
8f89e57a 247 }
70ee1be2 248
e76da505 249 if (ret_chan && idx == channel_idx) {
8f89e57a
JM
250 wpa_printf(MSG_DEBUG, "Selected channel %d (%d)",
251 chan->freq, chan->chan);
e76da505
JD
252 *ret_chan = chan;
253 return idx;
254 }
8f89e57a
JM
255 wpa_printf(MSG_DEBUG, "Adding channel %d (%d)",
256 chan->freq, chan->chan);
e76da505
JD
257 channel_idx++;
258 }
259 return channel_idx;
260}
261
262
7118a697
JC
263static void dfs_adjust_center_freq(struct hostapd_iface *iface,
264 struct hostapd_channel_data *chan,
265 int secondary_channel,
266 u8 *oper_centr_freq_seg0_idx,
267 u8 *oper_centr_freq_seg1_idx)
58b73e3d 268{
958cb348 269 if (!iface->conf->ieee80211ac && !iface->conf->ieee80211ax)
58b73e3d
JD
270 return;
271
272 if (!chan)
273 return;
274
7118a697 275 *oper_centr_freq_seg1_idx = 0;
32595da6 276
c6b7ac07 277 switch (hostapd_get_oper_chwidth(iface->conf)) {
464dcfd0 278 case CHANWIDTH_USE_HT:
eed65aad 279 if (secondary_channel == 1)
7118a697 280 *oper_centr_freq_seg0_idx = chan->chan + 2;
eed65aad 281 else if (secondary_channel == -1)
7118a697 282 *oper_centr_freq_seg0_idx = chan->chan - 2;
345276a6 283 else
7118a697 284 *oper_centr_freq_seg0_idx = chan->chan;
58b73e3d 285 break;
464dcfd0 286 case CHANWIDTH_80MHZ:
7118a697 287 *oper_centr_freq_seg0_idx = chan->chan + 6;
58b73e3d 288 break;
464dcfd0 289 case CHANWIDTH_160MHZ:
7118a697 290 *oper_centr_freq_seg0_idx = chan->chan + 14;
35f83637 291 break;
58b73e3d 292 default:
899cc14e 293 wpa_printf(MSG_INFO, "DFS only VHT20/40/80/160 is supported now");
7118a697 294 *oper_centr_freq_seg0_idx = 0;
58b73e3d
JD
295 break;
296 }
297
32595da6 298 wpa_printf(MSG_DEBUG, "DFS adjusting VHT center frequency: %d, %d",
7118a697
JC
299 *oper_centr_freq_seg0_idx,
300 *oper_centr_freq_seg1_idx);
58b73e3d
JD
301}
302
303
304/* Return start channel idx we will use for mode->channels[idx] */
4a274f48 305static int dfs_get_start_chan_idx(struct hostapd_iface *iface, int *seg1_start)
58b73e3d
JD
306{
307 struct hostapd_hw_modes *mode;
308 struct hostapd_channel_data *chan;
dc036d9e 309 int channel_no = iface->conf->channel;
58b73e3d 310 int res = -1, i;
4a274f48
JM
311 int chan_seg1 = -1;
312
313 *seg1_start = -1;
58b73e3d
JD
314
315 /* HT40- */
dc036d9e 316 if (iface->conf->ieee80211n && iface->conf->secondary_channel == -1)
58b73e3d
JD
317 channel_no -= 4;
318
958cb348
JC
319 /* VHT/HE */
320 if (iface->conf->ieee80211ac || iface->conf->ieee80211ax) {
c6b7ac07 321 switch (hostapd_get_oper_chwidth(iface->conf)) {
464dcfd0 322 case CHANWIDTH_USE_HT:
58b73e3d 323 break;
464dcfd0 324 case CHANWIDTH_80MHZ:
c6b7ac07
JC
325 channel_no = hostapd_get_oper_centr_freq_seg0_idx(
326 iface->conf) - 6;
58b73e3d 327 break;
464dcfd0 328 case CHANWIDTH_160MHZ:
c6b7ac07
JC
329 channel_no = hostapd_get_oper_centr_freq_seg0_idx(
330 iface->conf) - 14;
899cc14e 331 break;
464dcfd0 332 case CHANWIDTH_80P80MHZ:
c6b7ac07
JC
333 channel_no = hostapd_get_oper_centr_freq_seg0_idx(
334 iface->conf) - 6;
335 chan_seg1 = hostapd_get_oper_centr_freq_seg1_idx(
336 iface->conf) - 6;
4a274f48 337 break;
58b73e3d
JD
338 default:
339 wpa_printf(MSG_INFO,
4a274f48 340 "DFS only VHT20/40/80/160/80+80 is supported now");
58b73e3d
JD
341 channel_no = -1;
342 break;
343 }
344 }
345
346 /* Get idx */
dc036d9e 347 mode = iface->current_mode;
58b73e3d
JD
348 for (i = 0; i < mode->num_channels; i++) {
349 chan = &mode->channels[i];
350 if (chan->chan == channel_no) {
351 res = i;
352 break;
353 }
354 }
355
4a274f48
JM
356 if (res != -1 && chan_seg1 > -1) {
357 int found = 0;
358
359 /* Get idx for seg1 */
360 mode = iface->current_mode;
361 for (i = 0; i < mode->num_channels; i++) {
362 chan = &mode->channels[i];
363 if (chan->chan == chan_seg1) {
364 *seg1_start = i;
365 found = 1;
366 break;
367 }
368 }
369 if (!found)
370 res = -1;
371 }
372
7450c128
BG
373 if (res == -1) {
374 wpa_printf(MSG_DEBUG,
375 "DFS chan_idx seems wrong; num-ch: %d ch-no: %d conf-ch-no: %d 11n: %d sec-ch: %d vht-oper-width: %d",
376 mode->num_channels, channel_no, iface->conf->channel,
377 iface->conf->ieee80211n,
378 iface->conf->secondary_channel,
c6b7ac07 379 hostapd_get_oper_chwidth(iface->conf));
7450c128
BG
380
381 for (i = 0; i < mode->num_channels; i++) {
382 wpa_printf(MSG_DEBUG, "Available channel: %d",
383 mode->channels[i].chan);
384 }
385 }
58b73e3d
JD
386
387 return res;
388}
389
390
391/* At least one channel have radar flag */
dc036d9e
JM
392static int dfs_check_chans_radar(struct hostapd_iface *iface,
393 int start_chan_idx, int n_chans)
58b73e3d
JD
394{
395 struct hostapd_channel_data *channel;
396 struct hostapd_hw_modes *mode;
397 int i, res = 0;
398
dc036d9e 399 mode = iface->current_mode;
58b73e3d
JD
400
401 for (i = 0; i < n_chans; i++) {
402 channel = &mode->channels[start_chan_idx + i];
403 if (channel->flag & HOSTAPD_CHAN_RADAR)
404 res++;
405 }
406
407 return res;
408}
409
410
411/* All channels available */
dc036d9e 412static int dfs_check_chans_available(struct hostapd_iface *iface,
58b73e3d
JD
413 int start_chan_idx, int n_chans)
414{
415 struct hostapd_channel_data *channel;
416 struct hostapd_hw_modes *mode;
417 int i;
418
dc036d9e 419 mode = iface->current_mode;
58b73e3d 420
8c9cb81f 421 for (i = 0; i < n_chans; i++) {
58b73e3d 422 channel = &mode->channels[start_chan_idx + i];
b8058a69
JD
423
424 if (channel->flag & HOSTAPD_CHAN_DISABLED)
425 break;
426
427 if (!(channel->flag & HOSTAPD_CHAN_RADAR))
428 continue;
429
58b73e3d
JD
430 if ((channel->flag & HOSTAPD_CHAN_DFS_MASK) !=
431 HOSTAPD_CHAN_DFS_AVAILABLE)
432 break;
433 }
434
435 return i == n_chans;
436}
437
438
439/* At least one channel unavailable */
dc036d9e 440static int dfs_check_chans_unavailable(struct hostapd_iface *iface,
58b73e3d
JD
441 int start_chan_idx,
442 int n_chans)
443{
444 struct hostapd_channel_data *channel;
445 struct hostapd_hw_modes *mode;
446 int i, res = 0;
447
dc036d9e 448 mode = iface->current_mode;
58b73e3d 449
8c9cb81f 450 for (i = 0; i < n_chans; i++) {
58b73e3d
JD
451 channel = &mode->channels[start_chan_idx + i];
452 if (channel->flag & HOSTAPD_CHAN_DISABLED)
453 res++;
454 if ((channel->flag & HOSTAPD_CHAN_DFS_MASK) ==
455 HOSTAPD_CHAN_DFS_UNAVAILABLE)
456 res++;
457 }
458
459 return res;
460}
461
462
32595da6 463static struct hostapd_channel_data *
dc036d9e 464dfs_get_valid_channel(struct hostapd_iface *iface,
32595da6 465 int *secondary_channel,
7118a697
JC
466 u8 *oper_centr_freq_seg0_idx,
467 u8 *oper_centr_freq_seg1_idx,
b72f949b 468 int skip_radar)
e76da505
JD
469{
470 struct hostapd_hw_modes *mode;
471 struct hostapd_channel_data *chan = NULL;
32595da6
MK
472 int num_available_chandefs;
473 int chan_idx;
e76da505
JD
474 u32 _rand;
475
476 wpa_printf(MSG_DEBUG, "DFS: Selecting random channel");
3d91a047 477 *secondary_channel = 0;
7118a697
JC
478 *oper_centr_freq_seg0_idx = 0;
479 *oper_centr_freq_seg1_idx = 0;
e76da505 480
dc036d9e 481 if (iface->current_mode == NULL)
e76da505
JD
482 return NULL;
483
dc036d9e 484 mode = iface->current_mode;
e76da505
JD
485 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
486 return NULL;
487
32595da6 488 /* Get the count first */
b72f949b 489 num_available_chandefs = dfs_find_channel(iface, NULL, 0, skip_radar);
8f89e57a
JM
490 wpa_printf(MSG_DEBUG, "DFS: num_available_chandefs=%d",
491 num_available_chandefs);
32595da6
MK
492 if (num_available_chandefs == 0)
493 return NULL;
e76da505 494
7c0e5b58 495 if (os_get_random((u8 *) &_rand, sizeof(_rand)) < 0)
239952b4 496 return NULL;
32595da6 497 chan_idx = _rand % num_available_chandefs;
b72f949b 498 dfs_find_channel(iface, &chan, chan_idx, skip_radar);
8f89e57a
JM
499 if (chan)
500 wpa_printf(MSG_DEBUG, "DFS: got random channel %d (%d)",
501 chan->freq, chan->chan);
502 else
503 wpa_printf(MSG_DEBUG, "DFS: no random channel found");
32595da6
MK
504
505 /* dfs_find_channel() calculations assume HT40+ */
dc036d9e 506 if (iface->conf->secondary_channel)
32595da6
MK
507 *secondary_channel = 1;
508 else
509 *secondary_channel = 0;
510
7118a697
JC
511 dfs_adjust_center_freq(iface, chan,
512 *secondary_channel,
513 oper_centr_freq_seg0_idx,
514 oper_centr_freq_seg1_idx);
58b73e3d 515
e76da505
JD
516 return chan;
517}
518
519
dc036d9e 520static int set_dfs_state_freq(struct hostapd_iface *iface, int freq, u32 state)
e76da505
JD
521{
522 struct hostapd_hw_modes *mode;
523 struct hostapd_channel_data *chan = NULL;
524 int i;
525
dc036d9e 526 mode = iface->current_mode;
e76da505
JD
527 if (mode == NULL)
528 return 0;
529
58b73e3d 530 wpa_printf(MSG_DEBUG, "set_dfs_state 0x%X for %d MHz", state, freq);
dc036d9e
JM
531 for (i = 0; i < iface->current_mode->num_channels; i++) {
532 chan = &iface->current_mode->channels[i];
e76da505
JD
533 if (chan->freq == freq) {
534 if (chan->flag & HOSTAPD_CHAN_RADAR) {
535 chan->flag &= ~HOSTAPD_CHAN_DFS_MASK;
536 chan->flag |= state;
537 return 1; /* Channel found */
538 }
539 }
540 }
541 wpa_printf(MSG_WARNING, "Can't set DFS state for freq %d MHz", freq);
542 return 0;
543}
544
545
dc036d9e 546static int set_dfs_state(struct hostapd_iface *iface, int freq, int ht_enabled,
58b73e3d
JD
547 int chan_offset, int chan_width, int cf1,
548 int cf2, u32 state)
549{
550 int n_chans = 1, i;
551 struct hostapd_hw_modes *mode;
552 int frequency = freq;
530b8ee3 553 int frequency2 = 0;
58b73e3d
JD
554 int ret = 0;
555
dc036d9e 556 mode = iface->current_mode;
58b73e3d
JD
557 if (mode == NULL)
558 return 0;
559
560 if (mode->mode != HOSTAPD_MODE_IEEE80211A) {
561 wpa_printf(MSG_WARNING, "current_mode != IEEE80211A");
562 return 0;
563 }
564
565 /* Seems cf1 and chan_width is enough here */
566 switch (chan_width) {
567 case CHAN_WIDTH_20_NOHT:
568 case CHAN_WIDTH_20:
569 n_chans = 1;
bb337dda
JM
570 if (frequency == 0)
571 frequency = cf1;
58b73e3d
JD
572 break;
573 case CHAN_WIDTH_40:
574 n_chans = 2;
575 frequency = cf1 - 10;
576 break;
577 case CHAN_WIDTH_80:
578 n_chans = 4;
579 frequency = cf1 - 30;
580 break;
530b8ee3
LW
581 case CHAN_WIDTH_80P80:
582 n_chans = 4;
583 frequency = cf1 - 30;
584 frequency2 = cf2 - 30;
585 break;
899cc14e
JD
586 case CHAN_WIDTH_160:
587 n_chans = 8;
588 frequency = cf1 - 70;
589 break;
58b73e3d
JD
590 default:
591 wpa_printf(MSG_INFO, "DFS chan_width %d not supported",
592 chan_width);
593 break;
594 }
595
596 wpa_printf(MSG_DEBUG, "DFS freq: %dMHz, n_chans: %d", frequency,
597 n_chans);
598 for (i = 0; i < n_chans; i++) {
dc036d9e 599 ret += set_dfs_state_freq(iface, frequency, state);
58b73e3d 600 frequency = frequency + 20;
530b8ee3
LW
601
602 if (chan_width == CHAN_WIDTH_80P80) {
603 ret += set_dfs_state_freq(iface, frequency2, state);
604 frequency2 = frequency2 + 20;
605 }
58b73e3d
JD
606 }
607
608 return ret;
609}
610
611
dc036d9e 612static int dfs_are_channels_overlapped(struct hostapd_iface *iface, int freq,
58b73e3d
JD
613 int chan_width, int cf1, int cf2)
614{
4a274f48 615 int start_chan_idx, start_chan_idx1;
58b73e3d
JD
616 struct hostapd_hw_modes *mode;
617 struct hostapd_channel_data *chan;
4a274f48 618 int n_chans, n_chans1, i, j, frequency = freq, radar_n_chans = 1;
58b73e3d
JD
619 u8 radar_chan;
620 int res = 0;
621
58b73e3d 622 /* Our configuration */
dc036d9e 623 mode = iface->current_mode;
4a274f48
JM
624 start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1);
625 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
58b73e3d 626
5eaf240a 627 /* Check we are on DFS channel(s) */
dc036d9e 628 if (!dfs_check_chans_radar(iface, start_chan_idx, n_chans))
5eaf240a
JD
629 return 0;
630
58b73e3d
JD
631 /* Reported via radar event */
632 switch (chan_width) {
633 case CHAN_WIDTH_20_NOHT:
634 case CHAN_WIDTH_20:
635 radar_n_chans = 1;
bb337dda
JM
636 if (frequency == 0)
637 frequency = cf1;
58b73e3d
JD
638 break;
639 case CHAN_WIDTH_40:
640 radar_n_chans = 2;
641 frequency = cf1 - 10;
642 break;
643 case CHAN_WIDTH_80:
644 radar_n_chans = 4;
645 frequency = cf1 - 30;
646 break;
899cc14e
JD
647 case CHAN_WIDTH_160:
648 radar_n_chans = 8;
649 frequency = cf1 - 70;
650 break;
58b73e3d
JD
651 default:
652 wpa_printf(MSG_INFO, "DFS chan_width %d not supported",
653 chan_width);
654 break;
655 }
656
657 ieee80211_freq_to_chan(frequency, &radar_chan);
658
659 for (i = 0; i < n_chans; i++) {
660 chan = &mode->channels[start_chan_idx + i];
5eaf240a
JD
661 if (!(chan->flag & HOSTAPD_CHAN_RADAR))
662 continue;
58b73e3d
JD
663 for (j = 0; j < radar_n_chans; j++) {
664 wpa_printf(MSG_DEBUG, "checking our: %d, radar: %d",
665 chan->chan, radar_chan + j * 4);
666 if (chan->chan == radar_chan + j * 4)
667 res++;
668 }
669 }
670
671 wpa_printf(MSG_DEBUG, "overlapped: %d", res);
672
673 return res;
674}
675
676
bbbacbf2
JD
677static unsigned int dfs_get_cac_time(struct hostapd_iface *iface,
678 int start_chan_idx, int n_chans)
679{
680 struct hostapd_channel_data *channel;
681 struct hostapd_hw_modes *mode;
682 int i;
683 unsigned int cac_time_ms = 0;
684
685 mode = iface->current_mode;
686
687 for (i = 0; i < n_chans; i++) {
688 channel = &mode->channels[start_chan_idx + i];
689 if (!(channel->flag & HOSTAPD_CHAN_RADAR))
690 continue;
691 if (channel->dfs_cac_ms > cac_time_ms)
692 cac_time_ms = channel->dfs_cac_ms;
693 }
694
695 return cac_time_ms;
696}
697
698
e76da505
JD
699/*
700 * Main DFS handler
701 * 1 - continue channel/ap setup
702 * 0 - channel/ap setup will be continued after CAC
703 * -1 - hit critical error
704 */
dc036d9e 705int hostapd_handle_dfs(struct hostapd_iface *iface)
e76da505 706{
e76da505 707 struct hostapd_channel_data *channel;
4a274f48 708 int res, n_chans, n_chans1, start_chan_idx, start_chan_idx1;
b72f949b 709 int skip_radar = 0;
58b73e3d 710
3cf360b8
AB
711 if (is_6ghz_freq(iface->freq))
712 return 1;
713
afbe57d9
JM
714 if (!iface->current_mode) {
715 /*
716 * This can happen with drivers that do not provide mode
717 * information and as such, cannot really use hostapd for DFS.
718 */
719 wpa_printf(MSG_DEBUG,
720 "DFS: No current_mode information - assume no need to perform DFS operations by hostapd");
721 return 1;
722 }
723
dc036d9e 724 iface->cac_started = 0;
954e71d2 725
58b73e3d
JD
726 do {
727 /* Get start (first) channel for current configuration */
4a274f48
JM
728 start_chan_idx = dfs_get_start_chan_idx(iface,
729 &start_chan_idx1);
58b73e3d
JD
730 if (start_chan_idx == -1)
731 return -1;
732
733 /* Get number of used channels, depend on width */
4a274f48 734 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
58b73e3d 735
bbbacbf2
JD
736 /* Setup CAC time */
737 iface->dfs_cac_ms = dfs_get_cac_time(iface, start_chan_idx,
738 n_chans);
739
58b73e3d 740 /* Check if any of configured channels require DFS */
dc036d9e 741 res = dfs_check_chans_radar(iface, start_chan_idx, n_chans);
58b73e3d
JD
742 wpa_printf(MSG_DEBUG,
743 "DFS %d channels required radar detection",
744 res);
745 if (!res)
746 return 1;
747
748 /* Check if all channels are DFS available */
dc036d9e 749 res = dfs_check_chans_available(iface, start_chan_idx, n_chans);
58b73e3d
JD
750 wpa_printf(MSG_DEBUG,
751 "DFS all channels available, (SKIP CAC): %s",
752 res ? "yes" : "no");
753 if (res)
754 return 1;
755
756 /* Check if any of configured channels is unavailable */
dc036d9e 757 res = dfs_check_chans_unavailable(iface, start_chan_idx,
58b73e3d
JD
758 n_chans);
759 wpa_printf(MSG_DEBUG, "DFS %d chans unavailable - choose other channel: %s",
760 res, res ? "yes": "no");
761 if (res) {
5479ff90
MS
762 int sec = 0;
763 u8 cf1 = 0, cf2 = 0;
32595da6 764
b72f949b
MK
765 channel = dfs_get_valid_channel(iface, &sec, &cf1, &cf2,
766 skip_radar);
e76da505
JD
767 if (!channel) {
768 wpa_printf(MSG_ERROR, "could not get valid channel");
3bd58861
ZK
769 hostapd_set_state(iface, HAPD_IFACE_DFS);
770 return 0;
e76da505 771 }
32595da6 772
dc036d9e
JM
773 iface->freq = channel->freq;
774 iface->conf->channel = channel->chan;
775 iface->conf->secondary_channel = sec;
c6b7ac07
JC
776 hostapd_set_oper_centr_freq_seg0_idx(iface->conf, cf1);
777 hostapd_set_oper_centr_freq_seg1_idx(iface->conf, cf2);
e76da505 778 }
58b73e3d
JD
779 } while (res);
780
781 /* Finally start CAC */
dc036d9e
JM
782 hostapd_set_state(iface, HAPD_IFACE_DFS);
783 wpa_printf(MSG_DEBUG, "DFS start CAC on %d MHz", iface->freq);
784 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_START
bbbacbf2 785 "freq=%d chan=%d sec_chan=%d, width=%d, seg0=%d, seg1=%d, cac_time=%ds",
dc036d9e 786 iface->freq,
1f374834 787 iface->conf->channel, iface->conf->secondary_channel,
c6b7ac07
JC
788 hostapd_get_oper_chwidth(iface->conf),
789 hostapd_get_oper_centr_freq_seg0_idx(iface->conf),
790 hostapd_get_oper_centr_freq_seg1_idx(iface->conf),
bbbacbf2 791 iface->dfs_cac_ms / 1000);
1f374834 792
c6b7ac07
JC
793 res = hostapd_start_dfs_cac(
794 iface, iface->conf->hw_mode, iface->freq, iface->conf->channel,
795 iface->conf->ieee80211n, iface->conf->ieee80211ac,
88005ee9 796 iface->conf->ieee80211ax,
c6b7ac07
JC
797 iface->conf->secondary_channel,
798 hostapd_get_oper_chwidth(iface->conf),
799 hostapd_get_oper_centr_freq_seg0_idx(iface->conf),
800 hostapd_get_oper_centr_freq_seg1_idx(iface->conf));
1f374834
JD
801
802 if (res) {
803 wpa_printf(MSG_ERROR, "DFS start_dfs_cac() failed, %d", res);
58b73e3d 804 return -1;
e76da505
JD
805 }
806
58b73e3d 807 return 0;
e76da505
JD
808}
809
810
3dcd735c
VT
811static int hostapd_config_dfs_chan_available(struct hostapd_iface *iface)
812{
813 int n_chans, n_chans1, start_chan_idx, start_chan_idx1;
814
815 /* Get the start (first) channel for current configuration */
816 start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1);
817 if (start_chan_idx < 0)
818 return 0;
819
820 /* Get the number of used channels, depending on width */
821 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
822
823 /* Check if all channels are DFS available */
824 return dfs_check_chans_available(iface, start_chan_idx, n_chans);
825}
826
827
dc036d9e 828int hostapd_dfs_complete_cac(struct hostapd_iface *iface, int success, int freq,
58b73e3d
JD
829 int ht_enabled, int chan_offset, int chan_width,
830 int cf1, int cf2)
e76da505 831{
dc036d9e 832 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_COMPLETED
186c9059
JM
833 "success=%d freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
834 success, freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
835
e76da505
JD
836 if (success) {
837 /* Complete iface/ap configuration */
5de81d7a
AK
838 if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) {
839 /* Complete AP configuration for the first bring up. */
840 if (iface->state != HAPD_IFACE_ENABLED)
841 hostapd_setup_interface_complete(iface, 0);
842 else
843 iface->cac_started = 0;
844 } else {
845 set_dfs_state(iface, freq, ht_enabled, chan_offset,
846 chan_width, cf1, cf2,
847 HOSTAPD_CHAN_DFS_AVAILABLE);
3dcd735c
VT
848 /*
849 * Just mark the channel available when CAC completion
850 * event is received in enabled state. CAC result could
851 * have been propagated from another radio having the
852 * same regulatory configuration. When CAC completion is
853 * received during non-HAPD_IFACE_ENABLED state, make
854 * sure the configured channel is available because this
855 * CAC completion event could have been propagated from
856 * another radio.
857 */
858 if (iface->state != HAPD_IFACE_ENABLED &&
859 hostapd_config_dfs_chan_available(iface)) {
860 hostapd_setup_interface_complete(iface, 0);
861 iface->cac_started = 0;
862 }
5de81d7a 863 }
e76da505
JD
864 }
865
866 return 0;
867}
868
869
7cbb5f1a
VT
870int hostapd_dfs_pre_cac_expired(struct hostapd_iface *iface, int freq,
871 int ht_enabled, int chan_offset, int chan_width,
872 int cf1, int cf2)
873{
874 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_PRE_CAC_EXPIRED
875 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
876 freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
877
878 /* Proceed only if DFS is not offloaded to the driver */
879 if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)
880 return 0;
881
882 set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
883 cf1, cf2, HOSTAPD_CHAN_DFS_USABLE);
884
885 return 0;
886}
887
888
4b37d242
SM
889static struct hostapd_channel_data *
890dfs_downgrade_bandwidth(struct hostapd_iface *iface, int *secondary_channel,
891 u8 *oper_centr_freq_seg0_idx,
892 u8 *oper_centr_freq_seg1_idx, int *skip_radar)
893{
894 struct hostapd_channel_data *channel;
895
896 for (;;) {
897 channel = dfs_get_valid_channel(iface, secondary_channel,
898 oper_centr_freq_seg0_idx,
899 oper_centr_freq_seg1_idx,
900 *skip_radar);
901 if (channel) {
902 wpa_printf(MSG_DEBUG, "DFS: Selected channel: %d",
903 channel->chan);
904 return channel;
905 }
906
907 if (*skip_radar) {
908 *skip_radar = 0;
909 } else {
910 if (iface->conf->vht_oper_chwidth == CHANWIDTH_USE_HT)
911 break;
912 *skip_radar = 1;
913 iface->conf->vht_oper_chwidth--;
914 }
915 }
916
917 wpa_printf(MSG_INFO,
918 "%s: no DFS channels left, waiting for NOP to finish",
919 __func__);
920 return NULL;
921}
922
923
f7154cee 924static int hostapd_dfs_start_channel_switch_cac(struct hostapd_iface *iface)
e76da505
JD
925{
926 struct hostapd_channel_data *channel;
f7154cee 927 int secondary_channel;
7118a697
JC
928 u8 oper_centr_freq_seg0_idx = 0;
929 u8 oper_centr_freq_seg1_idx = 0;
f7154cee 930 int skip_radar = 0;
e76da505 931 int err = 1;
f7154cee
JD
932
933 /* Radar detected during active CAC */
934 iface->cac_started = 0;
935 channel = dfs_get_valid_channel(iface, &secondary_channel,
7118a697
JC
936 &oper_centr_freq_seg0_idx,
937 &oper_centr_freq_seg1_idx,
f7154cee
JD
938 skip_radar);
939
940 if (!channel) {
4b37d242
SM
941 channel = dfs_downgrade_bandwidth(iface, &secondary_channel,
942 &oper_centr_freq_seg0_idx,
943 &oper_centr_freq_seg1_idx,
944 &skip_radar);
945 if (!channel) {
946 wpa_printf(MSG_ERROR, "No valid channel available");
947 return err;
948 }
f7154cee
JD
949 }
950
951 wpa_printf(MSG_DEBUG, "DFS will switch to a new channel %d",
952 channel->chan);
953 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NEW_CHANNEL
954 "freq=%d chan=%d sec_chan=%d", channel->freq,
955 channel->chan, secondary_channel);
956
957 iface->freq = channel->freq;
958 iface->conf->channel = channel->chan;
959 iface->conf->secondary_channel = secondary_channel;
c6b7ac07
JC
960 hostapd_set_oper_centr_freq_seg0_idx(iface->conf,
961 oper_centr_freq_seg0_idx);
962 hostapd_set_oper_centr_freq_seg1_idx(iface->conf,
963 oper_centr_freq_seg1_idx);
f7154cee
JD
964 err = 0;
965
966 hostapd_setup_interface_complete(iface, err);
967 return err;
968}
969
970
971static int hostapd_dfs_start_channel_switch(struct hostapd_iface *iface)
972{
973 struct hostapd_channel_data *channel;
32595da6 974 int secondary_channel;
7118a697
JC
975 u8 oper_centr_freq_seg0_idx;
976 u8 oper_centr_freq_seg1_idx;
4b37d242 977 u8 new_vht_oper_chwidth;
b72f949b 978 int skip_radar = 1;
f7154cee 979 struct csa_settings csa_settings;
8974620e 980 unsigned int i;
f7154cee 981 int err = 1;
29d8bd1d 982 struct hostapd_hw_modes *cmode = iface->current_mode;
4b37d242 983 u8 current_vht_oper_chwidth = iface->conf->vht_oper_chwidth;
f7154cee 984
25592b23
JD
985 wpa_printf(MSG_DEBUG, "%s called (CAC active: %s, CSA active: %s)",
986 __func__, iface->cac_started ? "yes" : "no",
6782b684 987 hostapd_csa_in_progress(iface) ? "yes" : "no");
25592b23
JD
988
989 /* Check if CSA in progress */
6782b684 990 if (hostapd_csa_in_progress(iface))
25592b23 991 return 0;
f7154cee
JD
992
993 /* Check if active CAC */
994 if (iface->cac_started)
995 return hostapd_dfs_start_channel_switch_cac(iface);
e76da505 996
04f667fc
VT
997 /*
998 * Allow selection of DFS channel in ETSI to comply with
999 * uniform spreading.
1000 */
1001 if (iface->dfs_domain == HOSTAPD_DFS_REGION_ETSI)
1002 skip_radar = 0;
1003
f7154cee 1004 /* Perform channel switch/CSA */
dc036d9e 1005 channel = dfs_get_valid_channel(iface, &secondary_channel,
7118a697
JC
1006 &oper_centr_freq_seg0_idx,
1007 &oper_centr_freq_seg1_idx,
b72f949b 1008 skip_radar);
813d4bac 1009
f7154cee 1010 if (!channel) {
e0700512
SW
1011 /*
1012 * If there is no channel to switch immediately to, check if
1013 * there is another channel where we can switch even if it
1014 * requires to perform a CAC first.
1015 */
1016 skip_radar = 0;
4b37d242
SM
1017 channel = dfs_downgrade_bandwidth(iface, &secondary_channel,
1018 &oper_centr_freq_seg0_idx,
1019 &oper_centr_freq_seg1_idx,
1020 &skip_radar);
1021 if (!channel)
e0700512 1022 return err;
4b37d242
SM
1023 if (!skip_radar) {
1024 iface->freq = channel->freq;
1025 iface->conf->channel = channel->chan;
1026 iface->conf->secondary_channel = secondary_channel;
1027 hostapd_set_oper_centr_freq_seg0_idx(
1028 iface->conf, oper_centr_freq_seg0_idx);
1029 hostapd_set_oper_centr_freq_seg1_idx(
1030 iface->conf, oper_centr_freq_seg1_idx);
1031
1032 hostapd_disable_iface(iface);
1033 hostapd_enable_iface(iface);
1034 return 0;
e0700512 1035 }
f7154cee
JD
1036 }
1037
1038 wpa_printf(MSG_DEBUG, "DFS will switch to a new channel %d",
1039 channel->chan);
1040 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NEW_CHANNEL
1041 "freq=%d chan=%d sec_chan=%d", channel->freq,
1042 channel->chan, secondary_channel);
1043
4b37d242
SM
1044 new_vht_oper_chwidth = iface->conf->vht_oper_chwidth;
1045 iface->conf->vht_oper_chwidth = current_vht_oper_chwidth;
1046
f7154cee
JD
1047 /* Setup CSA request */
1048 os_memset(&csa_settings, 0, sizeof(csa_settings));
1049 csa_settings.cs_count = 5;
1050 csa_settings.block_tx = 1;
1051 err = hostapd_set_freq_params(&csa_settings.freq_params,
1052 iface->conf->hw_mode,
1053 channel->freq,
1054 channel->chan,
bebd91e9
AAL
1055 iface->conf->enable_edmg,
1056 iface->conf->edmg_channel,
f7154cee
JD
1057 iface->conf->ieee80211n,
1058 iface->conf->ieee80211ac,
88005ee9 1059 iface->conf->ieee80211ax,
f7154cee 1060 secondary_channel,
4b37d242 1061 new_vht_oper_chwidth,
7118a697
JC
1062 oper_centr_freq_seg0_idx,
1063 oper_centr_freq_seg1_idx,
29d8bd1d
SE
1064 cmode->vht_capab,
1065 &cmode->he_capab[IEEE80211_MODE_AP]);
f7154cee
JD
1066
1067 if (err) {
1068 wpa_printf(MSG_ERROR, "DFS failed to calculate CSA freq params");
1069 hostapd_disable_iface(iface);
1070 return err;
1071 }
1072
8974620e
MK
1073 for (i = 0; i < iface->num_bss; i++) {
1074 err = hostapd_switch_channel(iface->bss[i], &csa_settings);
1075 if (err)
1076 break;
1077 }
1078
f7154cee
JD
1079 if (err) {
1080 wpa_printf(MSG_WARNING, "DFS failed to schedule CSA (%d) - trying fallback",
1081 err);
dc036d9e
JM
1082 iface->freq = channel->freq;
1083 iface->conf->channel = channel->chan;
1084 iface->conf->secondary_channel = secondary_channel;
4b37d242 1085 iface->conf->vht_oper_chwidth = new_vht_oper_chwidth;
c6b7ac07
JC
1086 hostapd_set_oper_centr_freq_seg0_idx(iface->conf,
1087 oper_centr_freq_seg0_idx);
1088 hostapd_set_oper_centr_freq_seg1_idx(iface->conf,
1089 oper_centr_freq_seg1_idx);
813d4bac 1090
dc036d9e 1091 hostapd_disable_iface(iface);
f7154cee
JD
1092 hostapd_enable_iface(iface);
1093 return 0;
2e946249 1094 }
e76da505 1095
f7154cee
JD
1096 /* Channel configuration will be updated once CSA completes and
1097 * ch_switch_notify event is received */
1098
1099 wpa_printf(MSG_DEBUG, "DFS waiting channel switch event");
e76da505
JD
1100 return 0;
1101}
58b73e3d
JD
1102
1103
dc036d9e 1104int hostapd_dfs_radar_detected(struct hostapd_iface *iface, int freq,
58b73e3d
JD
1105 int ht_enabled, int chan_offset, int chan_width,
1106 int cf1, int cf2)
1107{
1108 int res;
1109
dc036d9e 1110 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_RADAR_DETECTED
186c9059
JM
1111 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
1112 freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
1113
5de81d7a
AK
1114 /* Proceed only if DFS is not offloaded to the driver */
1115 if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)
1116 return 0;
1117
1118 if (!iface->conf->ieee80211h)
1119 return 0;
1120
58b73e3d 1121 /* mark radar frequency as invalid */
7242087d
SM
1122 res = set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
1123 cf1, cf2, HOSTAPD_CHAN_DFS_UNAVAILABLE);
1124 if (!res)
1125 return 0;
58b73e3d
JD
1126
1127 /* Skip if reported radar event not overlapped our channels */
dc036d9e 1128 res = dfs_are_channels_overlapped(iface, freq, chan_width, cf1, cf2);
58b73e3d
JD
1129 if (!res)
1130 return 0;
1131
58b73e3d 1132 /* radar detected while operating, switch the channel. */
dc036d9e 1133 res = hostapd_dfs_start_channel_switch(iface);
58b73e3d
JD
1134
1135 return res;
1136}
1137
1138
dc036d9e 1139int hostapd_dfs_nop_finished(struct hostapd_iface *iface, int freq,
58b73e3d
JD
1140 int ht_enabled, int chan_offset, int chan_width,
1141 int cf1, int cf2)
1142{
dc036d9e 1143 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NOP_FINISHED
186c9059
JM
1144 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
1145 freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
5de81d7a
AK
1146
1147 /* Proceed only if DFS is not offloaded to the driver */
1148 if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)
1149 return 0;
1150
58b73e3d 1151 /* TODO add correct implementation here */
dc036d9e
JM
1152 set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
1153 cf1, cf2, HOSTAPD_CHAN_DFS_USABLE);
3bd58861
ZK
1154
1155 /* Handle cases where all channels were initially unavailable */
1156 if (iface->state == HAPD_IFACE_DFS && !iface->cac_started)
1157 hostapd_handle_dfs(iface);
1158
58b73e3d
JD
1159 return 0;
1160}
3d7ad2f6
C
1161
1162
1163int hostapd_is_dfs_required(struct hostapd_iface *iface)
1164{
4a274f48 1165 int n_chans, n_chans1, start_chan_idx, start_chan_idx1, res;
3d7ad2f6 1166
8a0f3bf6
MP
1167 if (!iface->conf->ieee80211h || !iface->current_mode ||
1168 iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A)
1169 return 0;
3d7ad2f6
C
1170
1171 /* Get start (first) channel for current configuration */
4a274f48 1172 start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1);
3d7ad2f6
C
1173 if (start_chan_idx == -1)
1174 return -1;
1175
1176 /* Get number of used channels, depend on width */
4a274f48 1177 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
3d7ad2f6
C
1178
1179 /* Check if any of configured channels require DFS */
4a274f48
JM
1180 res = dfs_check_chans_radar(iface, start_chan_idx, n_chans);
1181 if (res)
1182 return res;
1183 if (start_chan_idx1 >= 0 && n_chans1 > 0)
1184 res = dfs_check_chans_radar(iface, start_chan_idx1, n_chans1);
1185 return res;
3d7ad2f6 1186}
c13578c3
AK
1187
1188
1189int hostapd_dfs_start_cac(struct hostapd_iface *iface, int freq,
1190 int ht_enabled, int chan_offset, int chan_width,
1191 int cf1, int cf2)
1192{
02292618
HW
1193 /* This is called when the driver indicates that an offloaded DFS has
1194 * started CAC. */
1195 hostapd_set_state(iface, HAPD_IFACE_DFS);
1196 /* TODO: How to check CAC time for ETSI weather channels? */
1197 iface->dfs_cac_ms = 60000;
c13578c3
AK
1198 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_START
1199 "freq=%d chan=%d chan_offset=%d width=%d seg0=%d "
1200 "seg1=%d cac_time=%ds",
02292618
HW
1201 freq, (freq - 5000) / 5, chan_offset, chan_width, cf1, cf2,
1202 iface->dfs_cac_ms / 1000);
c13578c3 1203 iface->cac_started = 1;
02292618 1204 os_get_reltime(&iface->dfs_cac_start);
c13578c3
AK
1205 return 0;
1206}
1207
1208
1209/*
1210 * Main DFS handler for offloaded case.
1211 * 2 - continue channel/AP setup for non-DFS channel
1212 * 1 - continue channel/AP setup for DFS channel
1213 * 0 - channel/AP setup will be continued after CAC
1214 * -1 - hit critical error
1215 */
1216int hostapd_handle_dfs_offload(struct hostapd_iface *iface)
1217{
1218 wpa_printf(MSG_DEBUG, "%s: iface->cac_started: %d",
1219 __func__, iface->cac_started);
1220
1221 /*
1222 * If DFS has already been started, then we are being called from a
1223 * callback to continue AP/channel setup. Reset the CAC start flag and
1224 * return.
1225 */
1226 if (iface->cac_started) {
1227 wpa_printf(MSG_DEBUG, "%s: iface->cac_started: %d",
1228 __func__, iface->cac_started);
1229 iface->cac_started = 0;
1230 return 1;
1231 }
1232
d239ab39 1233 if (ieee80211_is_dfs(iface->freq, iface->hw_features,
1234 iface->num_hw_features)) {
c13578c3
AK
1235 wpa_printf(MSG_DEBUG, "%s: freq %d MHz requires DFS",
1236 __func__, iface->freq);
1237 return 0;
1238 }
1239
1240 wpa_printf(MSG_DEBUG,
1241 "%s: freq %d MHz does not require DFS. Continue channel/AP setup",
1242 __func__, iface->freq);
1243 return 2;
1244}