]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/ap/dfs.c
tests: Fix trace-cmd output for host case
[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>
58b73e3d 4 * Copyright (c) 2013, 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"
186c9059 14#include "common/wpa_ctrl.h"
e76da505 15#include "hostapd.h"
e76da505
JD
16#include "ap_drv_ops.h"
17#include "drivers/driver.h"
18#include "dfs.h"
19
20
dc036d9e 21static int dfs_get_used_n_chans(struct hostapd_iface *iface)
58b73e3d
JD
22{
23 int n_chans = 1;
24
dc036d9e 25 if (iface->conf->ieee80211n && iface->conf->secondary_channel)
58b73e3d
JD
26 n_chans = 2;
27
dc036d9e
JM
28 if (iface->conf->ieee80211ac) {
29 switch (iface->conf->vht_oper_chwidth) {
58b73e3d
JD
30 case VHT_CHANWIDTH_USE_HT:
31 break;
32 case VHT_CHANWIDTH_80MHZ:
33 n_chans = 4;
34 break;
899cc14e
JD
35 case VHT_CHANWIDTH_160MHZ:
36 n_chans = 8;
37 break;
58b73e3d
JD
38 default:
39 break;
40 }
41 }
42
43 return n_chans;
44}
45
46
b72f949b
MK
47static int dfs_channel_available(struct hostapd_channel_data *chan,
48 int skip_radar)
58b73e3d 49{
b72f949b
MK
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
58b73e3d
JD
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
1dc17db3 68static int dfs_is_chan_allowed(struct hostapd_channel_data *chan, int n_chans)
58b73e3d 69{
1dc17db3
JD
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 };
4f1e01b8
JD
81 /*
82 * VHT160 valid channels based on center frequency:
83 * 50, 114
84 */
85 int allowed_160[] = { 36, 100 };
1dc17db3
JD
86 int *allowed = allowed_40;
87 unsigned int i, allowed_no = 0;
88
89 switch (n_chans) {
90 case 2:
91 allowed = allowed_40;
e7ecab4a 92 allowed_no = ARRAY_SIZE(allowed_40);
1dc17db3
JD
93 break;
94 case 4:
95 allowed = allowed_80;
e7ecab4a 96 allowed_no = ARRAY_SIZE(allowed_80);
1dc17db3 97 break;
4f1e01b8
JD
98 case 8:
99 allowed = allowed_160;
100 allowed_no = ARRAY_SIZE(allowed_160);
101 break;
1dc17db3
JD
102 default:
103 wpa_printf(MSG_DEBUG, "Unknown width for %d channels", n_chans);
104 break;
105 }
58b73e3d 106
1dc17db3 107 for (i = 0; i < allowed_no; i++) {
58b73e3d
JD
108 if (chan->chan == allowed[i])
109 return 1;
110 }
111
112 return 0;
113}
114
115
6a398ddc 116static int dfs_chan_range_available(struct hostapd_hw_modes *mode,
b72f949b
MK
117 int first_chan_idx, int num_chans,
118 int skip_radar)
6a398ddc
MK
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
b72f949b 134 if (!dfs_channel_available(chan, skip_radar))
6a398ddc
MK
135 return 0;
136 }
137
138 return 1;
139}
140
141
32595da6
MK
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 */
dc036d9e 149static int dfs_find_channel(struct hostapd_iface *iface,
6de0e0c9 150 struct hostapd_channel_data **ret_chan,
b72f949b 151 int idx, int skip_radar)
e76da505
JD
152{
153 struct hostapd_hw_modes *mode;
6a398ddc
MK
154 struct hostapd_channel_data *chan;
155 int i, channel_idx = 0, n_chans;
e76da505 156
dc036d9e
JM
157 mode = iface->current_mode;
158 n_chans = dfs_get_used_n_chans(iface);
e76da505 159
58b73e3d 160 wpa_printf(MSG_DEBUG, "DFS new chan checking %d channels", n_chans);
e76da505
JD
161 for (i = 0; i < mode->num_channels; i++) {
162 chan = &mode->channels[i];
163
6a398ddc 164 /* Skip HT40/VHT incompatible channels */
dc036d9e
JM
165 if (iface->conf->ieee80211n &&
166 iface->conf->secondary_channel &&
6a398ddc 167 !dfs_is_chan_allowed(chan, n_chans))
e76da505
JD
168 continue;
169
6a398ddc 170 /* Skip incompatible chandefs */
b72f949b 171 if (!dfs_chan_range_available(mode, i, n_chans, skip_radar))
6a398ddc 172 continue;
e76da505
JD
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 }
58b73e3d 179 wpa_printf(MSG_DEBUG, "Adding channel: %d", chan->chan);
e76da505
JD
180 channel_idx++;
181 }
182 return channel_idx;
183}
184
185
dc036d9e 186static void dfs_adjust_vht_center_freq(struct hostapd_iface *iface,
32595da6 187 struct hostapd_channel_data *chan,
eed65aad 188 int secondary_channel,
32595da6
MK
189 u8 *vht_oper_centr_freq_seg0_idx,
190 u8 *vht_oper_centr_freq_seg1_idx)
58b73e3d 191{
dc036d9e 192 if (!iface->conf->ieee80211ac)
58b73e3d
JD
193 return;
194
195 if (!chan)
196 return;
197
32595da6
MK
198 *vht_oper_centr_freq_seg1_idx = 0;
199
dc036d9e 200 switch (iface->conf->vht_oper_chwidth) {
58b73e3d 201 case VHT_CHANWIDTH_USE_HT:
eed65aad 202 if (secondary_channel == 1)
32595da6 203 *vht_oper_centr_freq_seg0_idx = chan->chan + 2;
eed65aad 204 else if (secondary_channel == -1)
32595da6 205 *vht_oper_centr_freq_seg0_idx = chan->chan - 2;
345276a6 206 else
32595da6 207 *vht_oper_centr_freq_seg0_idx = chan->chan;
58b73e3d
JD
208 break;
209 case VHT_CHANWIDTH_80MHZ:
32595da6 210 *vht_oper_centr_freq_seg0_idx = chan->chan + 6;
58b73e3d 211 break;
899cc14e 212 case VHT_CHANWIDTH_160MHZ:
32595da6 213 *vht_oper_centr_freq_seg0_idx = chan->chan + 14;
35f83637 214 break;
58b73e3d 215 default:
899cc14e 216 wpa_printf(MSG_INFO, "DFS only VHT20/40/80/160 is supported now");
3d91a047 217 *vht_oper_centr_freq_seg0_idx = 0;
58b73e3d
JD
218 break;
219 }
220
32595da6
MK
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);
58b73e3d
JD
224}
225
226
227/* Return start channel idx we will use for mode->channels[idx] */
dc036d9e 228static int dfs_get_start_chan_idx(struct hostapd_iface *iface)
58b73e3d
JD
229{
230 struct hostapd_hw_modes *mode;
231 struct hostapd_channel_data *chan;
dc036d9e 232 int channel_no = iface->conf->channel;
58b73e3d
JD
233 int res = -1, i;
234
235 /* HT40- */
dc036d9e 236 if (iface->conf->ieee80211n && iface->conf->secondary_channel == -1)
58b73e3d
JD
237 channel_no -= 4;
238
239 /* VHT */
dc036d9e
JM
240 if (iface->conf->ieee80211ac) {
241 switch (iface->conf->vht_oper_chwidth) {
58b73e3d
JD
242 case VHT_CHANWIDTH_USE_HT:
243 break;
244 case VHT_CHANWIDTH_80MHZ:
245 channel_no =
dc036d9e 246 iface->conf->vht_oper_centr_freq_seg0_idx - 6;
58b73e3d 247 break;
899cc14e
JD
248 case VHT_CHANWIDTH_160MHZ:
249 channel_no =
dc036d9e 250 iface->conf->vht_oper_centr_freq_seg0_idx - 14;
899cc14e 251 break;
58b73e3d
JD
252 default:
253 wpa_printf(MSG_INFO,
899cc14e 254 "DFS only VHT20/40/80/160 is supported now");
58b73e3d
JD
255 channel_no = -1;
256 break;
257 }
258 }
259
260 /* Get idx */
dc036d9e 261 mode = iface->current_mode;
58b73e3d
JD
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 */
dc036d9e
JM
278static int dfs_check_chans_radar(struct hostapd_iface *iface,
279 int start_chan_idx, int n_chans)
58b73e3d
JD
280{
281 struct hostapd_channel_data *channel;
282 struct hostapd_hw_modes *mode;
283 int i, res = 0;
284
dc036d9e 285 mode = iface->current_mode;
58b73e3d
JD
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 */
dc036d9e 298static int dfs_check_chans_available(struct hostapd_iface *iface,
58b73e3d
JD
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
dc036d9e 305 mode = iface->current_mode;
58b73e3d 306
8c9cb81f 307 for (i = 0; i < n_chans; i++) {
58b73e3d 308 channel = &mode->channels[start_chan_idx + i];
b8058a69
JD
309
310 if (channel->flag & HOSTAPD_CHAN_DISABLED)
311 break;
312
313 if (!(channel->flag & HOSTAPD_CHAN_RADAR))
314 continue;
315
58b73e3d
JD
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 */
dc036d9e 326static int dfs_check_chans_unavailable(struct hostapd_iface *iface,
58b73e3d
JD
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
dc036d9e 334 mode = iface->current_mode;
58b73e3d 335
8c9cb81f 336 for (i = 0; i < n_chans; i++) {
58b73e3d
JD
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
32595da6 349static struct hostapd_channel_data *
dc036d9e 350dfs_get_valid_channel(struct hostapd_iface *iface,
32595da6
MK
351 int *secondary_channel,
352 u8 *vht_oper_centr_freq_seg0_idx,
b72f949b
MK
353 u8 *vht_oper_centr_freq_seg1_idx,
354 int skip_radar)
e76da505
JD
355{
356 struct hostapd_hw_modes *mode;
357 struct hostapd_channel_data *chan = NULL;
32595da6
MK
358 int num_available_chandefs;
359 int chan_idx;
e76da505
JD
360 u32 _rand;
361
362 wpa_printf(MSG_DEBUG, "DFS: Selecting random channel");
3d91a047
JM
363 *secondary_channel = 0;
364 *vht_oper_centr_freq_seg0_idx = 0;
365 *vht_oper_centr_freq_seg1_idx = 0;
e76da505 366
dc036d9e 367 if (iface->current_mode == NULL)
e76da505
JD
368 return NULL;
369
dc036d9e 370 mode = iface->current_mode;
e76da505
JD
371 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
372 return NULL;
373
32595da6 374 /* Get the count first */
b72f949b 375 num_available_chandefs = dfs_find_channel(iface, NULL, 0, skip_radar);
32595da6
MK
376 if (num_available_chandefs == 0)
377 return NULL;
e76da505 378
32595da6
MK
379 os_get_random((u8 *) &_rand, sizeof(_rand));
380 chan_idx = _rand % num_available_chandefs;
b72f949b 381 dfs_find_channel(iface, &chan, chan_idx, skip_radar);
32595da6
MK
382
383 /* dfs_find_channel() calculations assume HT40+ */
dc036d9e 384 if (iface->conf->secondary_channel)
32595da6
MK
385 *secondary_channel = 1;
386 else
387 *secondary_channel = 0;
388
dc036d9e 389 dfs_adjust_vht_center_freq(iface, chan,
eed65aad 390 *secondary_channel,
32595da6
MK
391 vht_oper_centr_freq_seg0_idx,
392 vht_oper_centr_freq_seg1_idx);
58b73e3d 393
e76da505
JD
394 return chan;
395}
396
397
dc036d9e 398static int set_dfs_state_freq(struct hostapd_iface *iface, int freq, u32 state)
e76da505
JD
399{
400 struct hostapd_hw_modes *mode;
401 struct hostapd_channel_data *chan = NULL;
402 int i;
403
dc036d9e 404 mode = iface->current_mode;
e76da505
JD
405 if (mode == NULL)
406 return 0;
407
58b73e3d 408 wpa_printf(MSG_DEBUG, "set_dfs_state 0x%X for %d MHz", state, freq);
dc036d9e
JM
409 for (i = 0; i < iface->current_mode->num_channels; i++) {
410 chan = &iface->current_mode->channels[i];
e76da505
JD
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
dc036d9e 424static int set_dfs_state(struct hostapd_iface *iface, int freq, int ht_enabled,
58b73e3d
JD
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
dc036d9e 433 mode = iface->current_mode;
58b73e3d
JD
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;
bb337dda
JM
447 if (frequency == 0)
448 frequency = cf1;
58b73e3d
JD
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;
899cc14e
JD
458 case CHAN_WIDTH_160:
459 n_chans = 8;
460 frequency = cf1 - 70;
461 break;
58b73e3d
JD
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++) {
dc036d9e 471 ret += set_dfs_state_freq(iface, frequency, state);
58b73e3d
JD
472 frequency = frequency + 20;
473 }
474
475 return ret;
476}
477
478
dc036d9e 479static int dfs_are_channels_overlapped(struct hostapd_iface *iface, int freq,
58b73e3d
JD
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
58b73e3d 489 /* Our configuration */
dc036d9e
JM
490 mode = iface->current_mode;
491 start_chan_idx = dfs_get_start_chan_idx(iface);
492 n_chans = dfs_get_used_n_chans(iface);
58b73e3d 493
5eaf240a 494 /* Check we are on DFS channel(s) */
dc036d9e 495 if (!dfs_check_chans_radar(iface, start_chan_idx, n_chans))
5eaf240a
JD
496 return 0;
497
58b73e3d
JD
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;
bb337dda
JM
503 if (frequency == 0)
504 frequency = cf1;
58b73e3d
JD
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;
899cc14e
JD
514 case CHAN_WIDTH_160:
515 radar_n_chans = 8;
516 frequency = cf1 - 70;
517 break;
58b73e3d
JD
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];
5eaf240a
JD
528 if (!(chan->flag & HOSTAPD_CHAN_RADAR))
529 continue;
58b73e3d
JD
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
e76da505
JD
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 */
dc036d9e 550int hostapd_handle_dfs(struct hostapd_iface *iface)
e76da505 551{
e76da505 552 struct hostapd_channel_data *channel;
58b73e3d 553 int res, n_chans, start_chan_idx;
b72f949b 554 int skip_radar = 0;
58b73e3d 555
dc036d9e 556 iface->cac_started = 0;
954e71d2 557
58b73e3d
JD
558 do {
559 /* Get start (first) channel for current configuration */
dc036d9e 560 start_chan_idx = dfs_get_start_chan_idx(iface);
58b73e3d
JD
561 if (start_chan_idx == -1)
562 return -1;
563
564 /* Get number of used channels, depend on width */
dc036d9e 565 n_chans = dfs_get_used_n_chans(iface);
58b73e3d
JD
566
567 /* Check if any of configured channels require DFS */
dc036d9e 568 res = dfs_check_chans_radar(iface, start_chan_idx, n_chans);
58b73e3d
JD
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 */
dc036d9e 576 res = dfs_check_chans_available(iface, start_chan_idx, n_chans);
58b73e3d
JD
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 */
dc036d9e 584 res = dfs_check_chans_unavailable(iface, start_chan_idx,
58b73e3d
JD
585 n_chans);
586 wpa_printf(MSG_DEBUG, "DFS %d chans unavailable - choose other channel: %s",
587 res, res ? "yes": "no");
588 if (res) {
5479ff90
MS
589 int sec = 0;
590 u8 cf1 = 0, cf2 = 0;
32595da6 591
b72f949b
MK
592 channel = dfs_get_valid_channel(iface, &sec, &cf1, &cf2,
593 skip_radar);
e76da505
JD
594 if (!channel) {
595 wpa_printf(MSG_ERROR, "could not get valid channel");
596 return -1;
597 }
32595da6 598
dc036d9e
JM
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;
e76da505 604 }
58b73e3d
JD
605 } while (res);
606
607 /* Finally start CAC */
dc036d9e
JM
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
1f374834 611 "freq=%d chan=%d sec_chan=%d, width=%d, seg0=%d, seg1=%d",
dc036d9e 612 iface->freq,
1f374834
JD
613 iface->conf->channel, iface->conf->secondary_channel,
614 iface->conf->vht_oper_chwidth,
615 iface->conf->vht_oper_centr_freq_seg0_idx,
616 iface->conf->vht_oper_centr_freq_seg1_idx);
617
618 res = hostapd_start_dfs_cac(iface, iface->conf->hw_mode,
619 iface->freq,
620 iface->conf->channel,
621 iface->conf->ieee80211n,
622 iface->conf->ieee80211ac,
623 iface->conf->secondary_channel,
624 iface->conf->vht_oper_chwidth,
625 iface->conf->vht_oper_centr_freq_seg0_idx,
626 iface->conf->vht_oper_centr_freq_seg1_idx);
627
628 if (res) {
629 wpa_printf(MSG_ERROR, "DFS start_dfs_cac() failed, %d", res);
58b73e3d 630 return -1;
e76da505
JD
631 }
632
58b73e3d 633 return 0;
e76da505
JD
634}
635
636
dc036d9e 637int hostapd_dfs_complete_cac(struct hostapd_iface *iface, int success, int freq,
58b73e3d
JD
638 int ht_enabled, int chan_offset, int chan_width,
639 int cf1, int cf2)
e76da505 640{
dc036d9e 641 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_COMPLETED
186c9059
JM
642 "success=%d freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
643 success, freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
644
e76da505
JD
645 if (success) {
646 /* Complete iface/ap configuration */
dc036d9e 647 set_dfs_state(iface, freq, ht_enabled, chan_offset,
58b73e3d
JD
648 chan_width, cf1, cf2,
649 HOSTAPD_CHAN_DFS_AVAILABLE);
dc036d9e
JM
650 iface->cac_started = 0;
651 hostapd_setup_interface_complete(iface, 0);
e76da505
JD
652 }
653
654 return 0;
655}
656
657
f7154cee 658static int hostapd_dfs_start_channel_switch_cac(struct hostapd_iface *iface)
e76da505
JD
659{
660 struct hostapd_channel_data *channel;
f7154cee 661 int secondary_channel;
5479ff90
MS
662 u8 vht_oper_centr_freq_seg0_idx = 0;
663 u8 vht_oper_centr_freq_seg1_idx = 0;
f7154cee 664 int skip_radar = 0;
e76da505 665 int err = 1;
f7154cee
JD
666
667 /* Radar detected during active CAC */
668 iface->cac_started = 0;
669 channel = dfs_get_valid_channel(iface, &secondary_channel,
670 &vht_oper_centr_freq_seg0_idx,
671 &vht_oper_centr_freq_seg1_idx,
672 skip_radar);
673
674 if (!channel) {
675 wpa_printf(MSG_ERROR, "No valid channel available");
676 hostapd_setup_interface_complete(iface, err);
677 return err;
678 }
679
680 wpa_printf(MSG_DEBUG, "DFS will switch to a new channel %d",
681 channel->chan);
682 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NEW_CHANNEL
683 "freq=%d chan=%d sec_chan=%d", channel->freq,
684 channel->chan, secondary_channel);
685
686 iface->freq = channel->freq;
687 iface->conf->channel = channel->chan;
688 iface->conf->secondary_channel = secondary_channel;
689 iface->conf->vht_oper_centr_freq_seg0_idx =
690 vht_oper_centr_freq_seg0_idx;
691 iface->conf->vht_oper_centr_freq_seg1_idx =
692 vht_oper_centr_freq_seg1_idx;
693 err = 0;
694
695 hostapd_setup_interface_complete(iface, err);
696 return err;
697}
698
699
700static int hostapd_dfs_start_channel_switch(struct hostapd_iface *iface)
701{
702 struct hostapd_channel_data *channel;
32595da6
MK
703 int secondary_channel;
704 u8 vht_oper_centr_freq_seg0_idx;
705 u8 vht_oper_centr_freq_seg1_idx;
b72f949b 706 int skip_radar = 1;
f7154cee
JD
707 struct csa_settings csa_settings;
708 struct hostapd_data *hapd = iface->bss[0];
709 int err = 1;
710
25592b23
JD
711 wpa_printf(MSG_DEBUG, "%s called (CAC active: %s, CSA active: %s)",
712 __func__, iface->cac_started ? "yes" : "no",
713 iface->csa_in_progress ? "yes" : "no");
714
715 /* Check if CSA in progress */
716 if (iface->csa_in_progress)
717 return 0;
f7154cee
JD
718
719 /* Check if active CAC */
720 if (iface->cac_started)
721 return hostapd_dfs_start_channel_switch_cac(iface);
e76da505 722
f7154cee 723 /* Perform channel switch/CSA */
dc036d9e 724 channel = dfs_get_valid_channel(iface, &secondary_channel,
32595da6 725 &vht_oper_centr_freq_seg0_idx,
b72f949b
MK
726 &vht_oper_centr_freq_seg1_idx,
727 skip_radar);
813d4bac 728
f7154cee
JD
729 if (!channel) {
730 /* FIXME: Wait for channel(s) to become available */
731 hostapd_disable_iface(iface);
732 return err;
733 }
734
735 wpa_printf(MSG_DEBUG, "DFS will switch to a new channel %d",
736 channel->chan);
737 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NEW_CHANNEL
738 "freq=%d chan=%d sec_chan=%d", channel->freq,
739 channel->chan, secondary_channel);
740
741 /* Setup CSA request */
742 os_memset(&csa_settings, 0, sizeof(csa_settings));
743 csa_settings.cs_count = 5;
744 csa_settings.block_tx = 1;
745 err = hostapd_set_freq_params(&csa_settings.freq_params,
746 iface->conf->hw_mode,
747 channel->freq,
748 channel->chan,
749 iface->conf->ieee80211n,
750 iface->conf->ieee80211ac,
751 secondary_channel,
752 iface->conf->vht_oper_chwidth,
753 vht_oper_centr_freq_seg0_idx,
754 vht_oper_centr_freq_seg1_idx,
755 iface->current_mode->vht_capab);
756
757 if (err) {
758 wpa_printf(MSG_ERROR, "DFS failed to calculate CSA freq params");
759 hostapd_disable_iface(iface);
760 return err;
761 }
762
763 err = hostapd_switch_channel(hapd, &csa_settings);
764 if (err) {
765 wpa_printf(MSG_WARNING, "DFS failed to schedule CSA (%d) - trying fallback",
766 err);
dc036d9e
JM
767 iface->freq = channel->freq;
768 iface->conf->channel = channel->chan;
769 iface->conf->secondary_channel = secondary_channel;
770 iface->conf->vht_oper_centr_freq_seg0_idx =
771 vht_oper_centr_freq_seg0_idx;
772 iface->conf->vht_oper_centr_freq_seg1_idx =
773 vht_oper_centr_freq_seg1_idx;
813d4bac 774
dc036d9e 775 hostapd_disable_iface(iface);
f7154cee
JD
776 hostapd_enable_iface(iface);
777 return 0;
2e946249 778 }
e76da505 779
f7154cee
JD
780 /* Channel configuration will be updated once CSA completes and
781 * ch_switch_notify event is received */
782
783 wpa_printf(MSG_DEBUG, "DFS waiting channel switch event");
e76da505
JD
784 return 0;
785}
58b73e3d
JD
786
787
dc036d9e 788int hostapd_dfs_radar_detected(struct hostapd_iface *iface, int freq,
58b73e3d
JD
789 int ht_enabled, int chan_offset, int chan_width,
790 int cf1, int cf2)
791{
792 int res;
793
dc036d9e 794 if (!iface->conf->ieee80211h)
58b73e3d
JD
795 return 0;
796
dc036d9e 797 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_RADAR_DETECTED
186c9059
JM
798 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
799 freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
800
58b73e3d 801 /* mark radar frequency as invalid */
dc036d9e 802 res = set_dfs_state(iface, freq, ht_enabled, chan_offset,
58b73e3d
JD
803 chan_width, cf1, cf2,
804 HOSTAPD_CHAN_DFS_UNAVAILABLE);
805
806 /* Skip if reported radar event not overlapped our channels */
dc036d9e 807 res = dfs_are_channels_overlapped(iface, freq, chan_width, cf1, cf2);
58b73e3d
JD
808 if (!res)
809 return 0;
810
58b73e3d 811 /* radar detected while operating, switch the channel. */
dc036d9e 812 res = hostapd_dfs_start_channel_switch(iface);
58b73e3d
JD
813
814 return res;
815}
816
817
dc036d9e 818int hostapd_dfs_nop_finished(struct hostapd_iface *iface, int freq,
58b73e3d
JD
819 int ht_enabled, int chan_offset, int chan_width,
820 int cf1, int cf2)
821{
dc036d9e 822 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NOP_FINISHED
186c9059
JM
823 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
824 freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
58b73e3d 825 /* TODO add correct implementation here */
dc036d9e
JM
826 set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
827 cf1, cf2, HOSTAPD_CHAN_DFS_USABLE);
58b73e3d
JD
828 return 0;
829}
3d7ad2f6
C
830
831
832int hostapd_is_dfs_required(struct hostapd_iface *iface)
833{
834 int n_chans, start_chan_idx;
835
836 if (!iface->current_mode)
837 return -1;
838
839 /* Get start (first) channel for current configuration */
840 start_chan_idx = dfs_get_start_chan_idx(iface);
841 if (start_chan_idx == -1)
842 return -1;
843
844 /* Get number of used channels, depend on width */
845 n_chans = dfs_get_used_n_chans(iface);
846
847 /* Check if any of configured channels require DFS */
848 return dfs_check_chans_radar(iface, start_chan_idx, n_chans);
849}