1 /*
2 * DFS - Dynamic Frequency Selection
3 * Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi>
4 * Copyright (c) 2013-2017, 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/hw_features_common.h"
15 #include "common/wpa_ctrl.h"
16 #include "hostapd.h"
17 #include "ap_drv_ops.h"
18 #include "drivers/driver.h"
19 #include "dfs.h"
20
21
22 enum dfs_channel_type {
23 DFS_ANY_CHANNEL,
24 DFS_AVAILABLE, /* non-radar or radar-available */
25 DFS_NO_CAC_YET, /* radar-not-yet-available */
26 };
27
28 static struct hostapd_channel_data *
29 dfs_downgrade_bandwidth(struct hostapd_iface *iface, int *secondary_channel,
30 u8 *oper_centr_freq_seg0_idx,
31 u8 *oper_centr_freq_seg1_idx,
32 enum dfs_channel_type *channel_type);
33
34
dfs_use_radar_background(struct hostapd_iface * iface)35 static bool dfs_use_radar_background(struct hostapd_iface *iface)
36 {
37 return (iface->drv_flags2 & WPA_DRIVER_FLAGS2_RADAR_BACKGROUND) &&
38 iface->conf->enable_background_radar;
39 }
40
41
dfs_get_used_n_chans(struct hostapd_iface * iface,int * seg1)42 static int dfs_get_used_n_chans(struct hostapd_iface *iface, int *seg1)
43 {
44 int n_chans = 1;
45
46 *seg1 = 0;
47
48 if (iface->conf->ieee80211n && iface->conf->secondary_channel)
49 n_chans = 2;
50
51 if (iface->conf->ieee80211ac || iface->conf->ieee80211ax) {
52 switch (hostapd_get_oper_chwidth(iface->conf)) {
53 case CONF_OPER_CHWIDTH_USE_HT:
54 break;
55 case CONF_OPER_CHWIDTH_80MHZ:
56 n_chans = 4;
57 break;
58 case CONF_OPER_CHWIDTH_160MHZ:
59 n_chans = 8;
60 break;
61 case CONF_OPER_CHWIDTH_80P80MHZ:
62 n_chans = 4;
63 *seg1 = 4;
64 break;
65 default:
66 break;
67 }
68 }
69
70 return n_chans;
71 }
72
73
74 /* dfs_channel_available: select new channel according to type parameter */
dfs_channel_available(struct hostapd_channel_data * chan,enum dfs_channel_type type)75 static int dfs_channel_available(struct hostapd_channel_data *chan,
76 enum dfs_channel_type type)
77 {
78 if (type == DFS_NO_CAC_YET) {
79 /* Select only radar channel where CAC has not been
80 * performed yet
81 */
82 if ((chan->flag & HOSTAPD_CHAN_RADAR) &&
83 (chan->flag & HOSTAPD_CHAN_DFS_MASK) ==
84 HOSTAPD_CHAN_DFS_USABLE)
85 return 1;
86 return 0;
87 }
88
89 /*
90 * When radar detection happens, CSA is performed. However, there's no
91 * time for CAC, so radar channels must be skipped when finding a new
92 * channel for CSA, unless they are available for immediate use.
93 */
94 if (type == DFS_AVAILABLE && (chan->flag & HOSTAPD_CHAN_RADAR) &&
95 ((chan->flag & HOSTAPD_CHAN_DFS_MASK) !=
96 HOSTAPD_CHAN_DFS_AVAILABLE))
97 return 0;
98
99 if (chan->flag & HOSTAPD_CHAN_DISABLED)
100 return 0;
101 if ((chan->flag & HOSTAPD_CHAN_RADAR) &&
102 ((chan->flag & HOSTAPD_CHAN_DFS_MASK) ==
103 HOSTAPD_CHAN_DFS_UNAVAILABLE))
104 return 0;
105 return 1;
106 }
107
108
dfs_is_chan_allowed(struct hostapd_channel_data * chan,int n_chans)109 static int dfs_is_chan_allowed(struct hostapd_channel_data *chan, int n_chans)
110 {
111 /*
112 * The tables contain first valid channel number based on channel width.
113 * We will also choose this first channel as the control one.
114 */
115 int allowed_40[] = { 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157,
116 165, 173, 184, 192 };
117 /*
118 * VHT80, valid channels based on center frequency:
119 * 42, 58, 106, 122, 138, 155, 171
120 */
121 int allowed_80[] = { 36, 52, 100, 116, 132, 149, 165 };
122 /*
123 * VHT160 valid channels based on center frequency:
124 * 50, 114, 163
125 */
126 int allowed_160[] = { 36, 100, 149 };
127 int *allowed = allowed_40;
128 unsigned int i, allowed_no = 0;
129
130 switch (n_chans) {
131 case 2:
132 allowed = allowed_40;
133 allowed_no = ARRAY_SIZE(allowed_40);
134 break;
135 case 4:
136 allowed = allowed_80;
137 allowed_no = ARRAY_SIZE(allowed_80);
138 break;
139 case 8:
140 allowed = allowed_160;
141 allowed_no = ARRAY_SIZE(allowed_160);
142 break;
143 default:
144 wpa_printf(MSG_DEBUG, "Unknown width for %d channels", n_chans);
145 break;
146 }
147
148 for (i = 0; i < allowed_no; i++) {
149 if (chan->chan == allowed[i])
150 return 1;
151 }
152
153 return 0;
154 }
155
156
157 static struct hostapd_channel_data *
dfs_get_chan_data(struct hostapd_hw_modes * mode,int freq,int first_chan_idx)158 dfs_get_chan_data(struct hostapd_hw_modes *mode, int freq, int first_chan_idx)
159 {
160 int i;
161
162 for (i = first_chan_idx; i < mode->num_channels; i++) {
163 if (mode->channels[i].freq == freq)
164 return &mode->channels[i];
165 }
166
167 return NULL;
168 }
169
170
dfs_chan_range_available(struct hostapd_hw_modes * mode,int first_chan_idx,int num_chans,enum dfs_channel_type type)171 static int dfs_chan_range_available(struct hostapd_hw_modes *mode,
172 int first_chan_idx, int num_chans,
173 enum dfs_channel_type type)
174 {
175 struct hostapd_channel_data *first_chan, *chan;
176 int i;
177 u32 bw = num_chan_to_bw(num_chans);
178
179 if (first_chan_idx + num_chans > mode->num_channels) {
180 wpa_printf(MSG_DEBUG,
181 "DFS: some channels in range not defined");
182 return 0;
183 }
184
185 first_chan = &mode->channels[first_chan_idx];
186
187 /* hostapd DFS implementation assumes the first channel as primary.
188 * If it's not allowed to use the first channel as primary, decline the
189 * whole channel range. */
190 if (!chan_pri_allowed(first_chan)) {
191 wpa_printf(MSG_DEBUG, "DFS: primary chanenl not allowed");
192 return 0;
193 }
194
195 for (i = 0; i < num_chans; i++) {
196 chan = dfs_get_chan_data(mode, first_chan->freq + i * 20,
197 first_chan_idx);
198 if (!chan) {
199 wpa_printf(MSG_DEBUG, "DFS: no channel data for %d",
200 first_chan->freq + i * 20);
201 return 0;
202 }
203
204 /* HT 40 MHz secondary channel availability checked only for
205 * primary channel */
206 if (!chan_bw_allowed(chan, bw, 1, !i)) {
207 wpa_printf(MSG_DEBUG, "DFS: bw now allowed for %d",
208 first_chan->freq + i * 20);
209 return 0;
210 }
211
212 if (!dfs_channel_available(chan, type)) {
213 wpa_printf(MSG_DEBUG, "DFS: channel not available %d",
214 first_chan->freq + i * 20);
215 return 0;
216 }
217 }
218
219 return 1;
220 }
221
222
is_in_chanlist(struct hostapd_iface * iface,struct hostapd_channel_data * chan)223 static int is_in_chanlist(struct hostapd_iface *iface,
224 struct hostapd_channel_data *chan)
225 {
226 if (!iface->conf->acs_ch_list.num)
227 return 1;
228
229 return freq_range_list_includes(&iface->conf->acs_ch_list, chan->chan);
230 }
231
232
233 /*
234 * The function assumes HT40+ operation.
235 * Make sure to adjust the following variables after calling this:
236 * - hapd->secondary_channel
237 * - hapd->vht/he_oper_centr_freq_seg0_idx
238 * - hapd->vht/he_oper_centr_freq_seg1_idx
239 */
dfs_find_channel(struct hostapd_iface * iface,struct hostapd_channel_data ** ret_chan,int idx,enum dfs_channel_type type)240 static int dfs_find_channel(struct hostapd_iface *iface,
241 struct hostapd_channel_data **ret_chan,
242 int idx, enum dfs_channel_type type)
243 {
244 struct hostapd_hw_modes *mode;
245 struct hostapd_channel_data *chan;
246 int i, channel_idx = 0, n_chans, n_chans1;
247
248 mode = iface->current_mode;
249 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
250
251 wpa_printf(MSG_DEBUG, "DFS new chan checking %d channels", n_chans);
252 for (i = 0; i < mode->num_channels; i++) {
253 chan = &mode->channels[i];
254
255 /* Skip HT40/VHT incompatible channels */
256 if (iface->conf->ieee80211n &&
257 iface->conf->secondary_channel &&
258 (!dfs_is_chan_allowed(chan, n_chans) ||
259 !(chan->allowed_bw & HOSTAPD_CHAN_WIDTH_40P))) {
260 wpa_printf(MSG_DEBUG,
261 "DFS: channel %d (%d) is incompatible",
262 chan->freq, chan->chan);
263 continue;
264 }
265
266 /* Skip incompatible chandefs */
267 if (!dfs_chan_range_available(mode, i, n_chans, type)) {
268 wpa_printf(MSG_DEBUG,
269 "DFS: range not available for %d (%d)",
270 chan->freq, chan->chan);
271 continue;
272 }
273
274 if (!is_in_chanlist(iface, chan)) {
275 wpa_printf(MSG_DEBUG,
276 "DFS: channel %d (%d) not in chanlist",
277 chan->freq, chan->chan);
278 continue;
279 }
280
281 if (chan->max_tx_power < iface->conf->min_tx_power)
282 continue;
283
284 if ((chan->flag & HOSTAPD_CHAN_INDOOR_ONLY) &&
285 iface->conf->country[2] == 0x4f)
286 continue;
287
288 if (ret_chan && idx == channel_idx) {
289 wpa_printf(MSG_DEBUG, "Selected channel %d (%d)",
290 chan->freq, chan->chan);
291 *ret_chan = chan;
292 return idx;
293 }
294 wpa_printf(MSG_DEBUG, "Adding channel %d (%d)",
295 chan->freq, chan->chan);
296 channel_idx++;
297 }
298 return channel_idx;
299 }
300
301
dfs_adjust_center_freq(struct hostapd_iface * iface,struct hostapd_channel_data * chan,int secondary_channel,int sec_chan_idx_80p80,u8 * oper_centr_freq_seg0_idx,u8 * oper_centr_freq_seg1_idx)302 static void dfs_adjust_center_freq(struct hostapd_iface *iface,
303 struct hostapd_channel_data *chan,
304 int secondary_channel,
305 int sec_chan_idx_80p80,
306 u8 *oper_centr_freq_seg0_idx,
307 u8 *oper_centr_freq_seg1_idx)
308 {
309 if (!iface->conf->ieee80211ac && !iface->conf->ieee80211ax)
310 return;
311
312 if (!chan)
313 return;
314
315 *oper_centr_freq_seg1_idx = 0;
316
317 switch (hostapd_get_oper_chwidth(iface->conf)) {
318 case CONF_OPER_CHWIDTH_USE_HT:
319 if (secondary_channel == 1)
320 *oper_centr_freq_seg0_idx = chan->chan + 2;
321 else if (secondary_channel == -1)
322 *oper_centr_freq_seg0_idx = chan->chan - 2;
323 else
324 *oper_centr_freq_seg0_idx = chan->chan;
325 break;
326 case CONF_OPER_CHWIDTH_80MHZ:
327 *oper_centr_freq_seg0_idx = chan->chan + 6;
328 break;
329 case CONF_OPER_CHWIDTH_160MHZ:
330 *oper_centr_freq_seg0_idx = chan->chan + 14;
331 break;
332 case CONF_OPER_CHWIDTH_80P80MHZ:
333 *oper_centr_freq_seg0_idx = chan->chan + 6;
334 *oper_centr_freq_seg1_idx = sec_chan_idx_80p80 + 6;
335 break;
336
337 default:
338 wpa_printf(MSG_INFO,
339 "DFS: Unsupported channel width configuration");
340 *oper_centr_freq_seg0_idx = 0;
341 break;
342 }
343
344 wpa_printf(MSG_DEBUG, "DFS adjusting VHT center frequency: %d, %d",
345 *oper_centr_freq_seg0_idx,
346 *oper_centr_freq_seg1_idx);
347 }
348
349
350 /* Return start channel idx we will use for mode->channels[idx] */
dfs_get_start_chan_idx(struct hostapd_iface * iface,int * seg1_start)351 static int dfs_get_start_chan_idx(struct hostapd_iface *iface, int *seg1_start)
352 {
353 struct hostapd_hw_modes *mode;
354 struct hostapd_channel_data *chan;
355 int channel_no = iface->conf->channel;
356 int res = -1, i;
357 int chan_seg1 = -1;
358
359 *seg1_start = -1;
360
361 /* HT40- */
362 if (iface->conf->ieee80211n && iface->conf->secondary_channel == -1)
363 channel_no -= 4;
364
365 /* VHT/HE/EHT */
366 if (iface->conf->ieee80211ac || iface->conf->ieee80211ax ||
367 iface->conf->ieee80211be) {
368 switch (hostapd_get_oper_chwidth(iface->conf)) {
369 case CONF_OPER_CHWIDTH_USE_HT:
370 break;
371 case CONF_OPER_CHWIDTH_80MHZ:
372 channel_no = hostapd_get_oper_centr_freq_seg0_idx(
373 iface->conf) - 6;
374 break;
375 case CONF_OPER_CHWIDTH_160MHZ:
376 channel_no = hostapd_get_oper_centr_freq_seg0_idx(
377 iface->conf) - 14;
378 break;
379 case CONF_OPER_CHWIDTH_80P80MHZ:
380 channel_no = hostapd_get_oper_centr_freq_seg0_idx(
381 iface->conf) - 6;
382 chan_seg1 = hostapd_get_oper_centr_freq_seg1_idx(
383 iface->conf) - 6;
384 break;
385 case CONF_OPER_CHWIDTH_320MHZ:
386 channel_no = hostapd_get_oper_centr_freq_seg0_idx(
387 iface->conf) - 30;
388 break;
389 default:
390 wpa_printf(MSG_INFO,
391 "DFS only EHT20/40/80/160/80+80/320 is supported now");
392 channel_no = -1;
393 break;
394 }
395 }
396
397 /* Get idx */
398 mode = iface->current_mode;
399 for (i = 0; i < mode->num_channels; i++) {
400 chan = &mode->channels[i];
401 if (chan->chan == channel_no) {
402 res = i;
403 break;
404 }
405 }
406
407 if (res != -1 && chan_seg1 > -1) {
408 int found = 0;
409
410 /* Get idx for seg1 */
411 mode = iface->current_mode;
412 for (i = 0; i < mode->num_channels; i++) {
413 chan = &mode->channels[i];
414 if (chan->chan == chan_seg1) {
415 *seg1_start = i;
416 found = 1;
417 break;
418 }
419 }
420 if (!found)
421 res = -1;
422 }
423
424 if (res == -1) {
425 wpa_printf(MSG_DEBUG,
426 "DFS chan_idx seems wrong; num-ch: %d ch-no: %d conf-ch-no: %d 11n: %d sec-ch: %d vht-oper-width: %d",
427 mode->num_channels, channel_no, iface->conf->channel,
428 iface->conf->ieee80211n,
429 iface->conf->secondary_channel,
430 hostapd_get_oper_chwidth(iface->conf));
431
432 for (i = 0; i < mode->num_channels; i++) {
433 wpa_printf(MSG_DEBUG, "Available channel: %d",
434 mode->channels[i].chan);
435 }
436 }
437
438 return res;
439 }
440
441
442 /* At least one channel have radar flag */
dfs_check_chans_radar(struct hostapd_iface * iface,int start_chan_idx,int n_chans)443 static int dfs_check_chans_radar(struct hostapd_iface *iface,
444 int start_chan_idx, int n_chans)
445 {
446 struct hostapd_channel_data *channel;
447 struct hostapd_hw_modes *mode;
448 int i, res = 0;
449
450 mode = iface->current_mode;
451
452 for (i = 0; i < n_chans; i++) {
453 if (start_chan_idx + i >= mode->num_channels)
454 break;
455 channel = &mode->channels[start_chan_idx + i];
456 if (channel->flag & HOSTAPD_CHAN_RADAR)
457 res++;
458 }
459
460 return res;
461 }
462
463
464 /* All channels available */
dfs_check_chans_available(struct hostapd_iface * iface,int start_chan_idx,int n_chans)465 static int dfs_check_chans_available(struct hostapd_iface *iface,
466 int start_chan_idx, int n_chans)
467 {
468 struct hostapd_channel_data *channel;
469 struct hostapd_hw_modes *mode;
470 int i;
471
472 mode = iface->current_mode;
473
474 for (i = 0; i < n_chans; i++) {
475 channel = &mode->channels[start_chan_idx + i];
476
477 if (channel->flag & HOSTAPD_CHAN_DISABLED)
478 break;
479
480 if (!(channel->flag & HOSTAPD_CHAN_RADAR))
481 continue;
482
483 if ((channel->flag & HOSTAPD_CHAN_DFS_MASK) !=
484 HOSTAPD_CHAN_DFS_AVAILABLE)
485 break;
486 }
487
488 return i == n_chans;
489 }
490
491
492 /* At least one channel unavailable */
dfs_check_chans_unavailable(struct hostapd_iface * iface,int start_chan_idx,int n_chans)493 static int dfs_check_chans_unavailable(struct hostapd_iface *iface,
494 int start_chan_idx,
495 int n_chans)
496 {
497 struct hostapd_channel_data *channel;
498 struct hostapd_hw_modes *mode;
499 int i, res = 0;
500
501 mode = iface->current_mode;
502
503 for (i = 0; i < n_chans; i++) {
504 channel = &mode->channels[start_chan_idx + i];
505 if (channel->flag & HOSTAPD_CHAN_DISABLED)
506 res++;
507 if ((channel->flag & HOSTAPD_CHAN_DFS_MASK) ==
508 HOSTAPD_CHAN_DFS_UNAVAILABLE)
509 res++;
510 }
511
512 return res;
513 }
514
515
516 static struct hostapd_channel_data *
dfs_get_valid_channel(struct hostapd_iface * iface,int * secondary_channel,u8 * oper_centr_freq_seg0_idx,u8 * oper_centr_freq_seg1_idx,enum dfs_channel_type type)517 dfs_get_valid_channel(struct hostapd_iface *iface,
518 int *secondary_channel,
519 u8 *oper_centr_freq_seg0_idx,
520 u8 *oper_centr_freq_seg1_idx,
521 enum dfs_channel_type type)
522 {
523 struct hostapd_hw_modes *mode;
524 struct hostapd_channel_data *chan = NULL;
525 struct hostapd_channel_data *chan2 = NULL;
526 int num_available_chandefs;
527 int chan_idx, chan_idx2;
528 int sec_chan_idx_80p80 = -1;
529 int i;
530 u32 _rand;
531
532 wpa_printf(MSG_DEBUG, "DFS: Selecting random channel");
533 *secondary_channel = 0;
534 *oper_centr_freq_seg0_idx = 0;
535 *oper_centr_freq_seg1_idx = 0;
536
537 if (iface->current_mode == NULL)
538 return NULL;
539
540 mode = iface->current_mode;
541 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
542 return NULL;
543
544 /* Get the count first */
545 num_available_chandefs = dfs_find_channel(iface, NULL, 0, type);
546 wpa_printf(MSG_DEBUG, "DFS: num_available_chandefs=%d",
547 num_available_chandefs);
548 if (num_available_chandefs == 0)
549 return NULL;
550
551 if (os_get_random((u8 *) &_rand, sizeof(_rand)) < 0)
552 return NULL;
553 chan_idx = _rand % num_available_chandefs;
554 dfs_find_channel(iface, &chan, chan_idx, type);
555 if (!chan) {
556 wpa_printf(MSG_DEBUG, "DFS: no random channel found");
557 return NULL;
558 }
559 wpa_printf(MSG_DEBUG, "DFS: got random channel %d (%d)",
560 chan->freq, chan->chan);
561
562 /* dfs_find_channel() calculations assume HT40+ */
563 if (iface->conf->secondary_channel)
564 *secondary_channel = 1;
565 else
566 *secondary_channel = 0;
567
568 /* Get secondary channel for HT80P80 */
569 if (hostapd_get_oper_chwidth(iface->conf) ==
570 CONF_OPER_CHWIDTH_80P80MHZ) {
571 if (num_available_chandefs <= 1) {
572 wpa_printf(MSG_ERROR,
573 "only 1 valid chan, can't support 80+80");
574 return NULL;
575 }
576
577 /*
578 * Loop all channels except channel1 to find a valid channel2
579 * that is not adjacent to channel1.
580 */
581 for (i = 0; i < num_available_chandefs - 1; i++) {
582 /* start from chan_idx + 1, end when chan_idx - 1 */
583 chan_idx2 = (chan_idx + 1 + i) % num_available_chandefs;
584 dfs_find_channel(iface, &chan2, chan_idx2, type);
585 if (chan2 && abs(chan2->chan - chan->chan) > 12) {
586 /* two channels are not adjacent */
587 sec_chan_idx_80p80 = chan2->chan;
588 wpa_printf(MSG_DEBUG,
589 "DFS: got second chan: %d (%d)",
590 chan2->freq, chan2->chan);
591 break;
592 }
593 }
594
595 /* Check if we got a valid secondary channel which is not
596 * adjacent to the first channel.
597 */
598 if (sec_chan_idx_80p80 == -1) {
599 wpa_printf(MSG_INFO,
600 "DFS: failed to get chan2 for 80+80");
601 return NULL;
602 }
603 }
604
605 dfs_adjust_center_freq(iface, chan,
606 *secondary_channel,
607 sec_chan_idx_80p80,
608 oper_centr_freq_seg0_idx,
609 oper_centr_freq_seg1_idx);
610
611 return chan;
612 }
613
614
dfs_set_valid_channel(struct hostapd_iface * iface,int skip_radar)615 static int dfs_set_valid_channel(struct hostapd_iface *iface, int skip_radar)
616 {
617 struct hostapd_channel_data *channel;
618 u8 cf1 = 0, cf2 = 0;
619 int sec = 0;
620
621 channel = dfs_get_valid_channel(iface, &sec, &cf1, &cf2,
622 skip_radar ? DFS_AVAILABLE :
623 DFS_ANY_CHANNEL);
624 if (!channel) {
625 wpa_printf(MSG_ERROR, "could not get valid channel");
626 return -1;
627 }
628
629 iface->freq = channel->freq;
630 iface->conf->channel = channel->chan;
631 iface->conf->secondary_channel = sec;
632 hostapd_set_oper_centr_freq_seg0_idx(iface->conf, cf1);
633 hostapd_set_oper_centr_freq_seg1_idx(iface->conf, cf2);
634
635 return 0;
636 }
637
638
set_dfs_state_freq(struct hostapd_iface * iface,int freq,u32 state)639 static int set_dfs_state_freq(struct hostapd_iface *iface, int freq, u32 state)
640 {
641 struct hostapd_hw_modes *mode;
642 struct hostapd_channel_data *chan = NULL;
643 int i;
644
645 mode = iface->current_mode;
646 if (mode == NULL)
647 return 0;
648
649 wpa_printf(MSG_DEBUG, "set_dfs_state 0x%X for %d MHz", state, freq);
650 for (i = 0; i < iface->current_mode->num_channels; i++) {
651 chan = &iface->current_mode->channels[i];
652 if (chan->freq == freq) {
653 if (chan->flag & HOSTAPD_CHAN_RADAR) {
654 chan->flag &= ~HOSTAPD_CHAN_DFS_MASK;
655 chan->flag |= state;
656 return 1; /* Channel found */
657 }
658 }
659 }
660 wpa_printf(MSG_WARNING, "Can't set DFS state for freq %d MHz", freq);
661 return 0;
662 }
663
664
set_dfs_state(struct hostapd_iface * iface,int freq,int ht_enabled,int chan_offset,int chan_width,int cf1,int cf2,u32 state)665 static int set_dfs_state(struct hostapd_iface *iface, int freq, int ht_enabled,
666 int chan_offset, int chan_width, int cf1,
667 int cf2, u32 state)
668 {
669 int n_chans = 1, i;
670 struct hostapd_hw_modes *mode;
671 int frequency = freq;
672 int frequency2 = 0;
673 int ret = 0;
674
675 mode = iface->current_mode;
676 if (mode == NULL)
677 return 0;
678
679 if (mode->mode != HOSTAPD_MODE_IEEE80211A) {
680 wpa_printf(MSG_WARNING, "current_mode != IEEE80211A");
681 return 0;
682 }
683
684 /* Seems cf1 and chan_width is enough here */
685 switch (chan_width) {
686 case CHAN_WIDTH_20_NOHT:
687 case CHAN_WIDTH_20:
688 n_chans = 1;
689 if (frequency == 0)
690 frequency = cf1;
691 break;
692 case CHAN_WIDTH_40:
693 n_chans = 2;
694 frequency = cf1 - 10;
695 break;
696 case CHAN_WIDTH_80:
697 n_chans = 4;
698 frequency = cf1 - 30;
699 break;
700 case CHAN_WIDTH_80P80:
701 n_chans = 4;
702 frequency = cf1 - 30;
703 frequency2 = cf2 - 30;
704 break;
705 case CHAN_WIDTH_160:
706 n_chans = 8;
707 frequency = cf1 - 70;
708 break;
709 default:
710 wpa_printf(MSG_INFO, "DFS chan_width %d not supported",
711 chan_width);
712 break;
713 }
714
715 wpa_printf(MSG_DEBUG, "DFS freq: %dMHz, n_chans: %d", frequency,
716 n_chans);
717 for (i = 0; i < n_chans; i++) {
718 ret += set_dfs_state_freq(iface, frequency, state);
719 frequency = frequency + 20;
720
721 if (chan_width == CHAN_WIDTH_80P80) {
722 ret += set_dfs_state_freq(iface, frequency2, state);
723 frequency2 = frequency2 + 20;
724 }
725 }
726
727 return ret;
728 }
729
730
dfs_are_channels_overlapped(struct hostapd_iface * iface,int freq,int chan_width,int cf1,int cf2)731 static int dfs_are_channels_overlapped(struct hostapd_iface *iface, int freq,
732 int chan_width, int cf1, int cf2)
733 {
734 int start_chan_idx, start_chan_idx1;
735 struct hostapd_hw_modes *mode;
736 struct hostapd_channel_data *chan;
737 int n_chans, n_chans1, i, j, frequency = freq, radar_n_chans = 1;
738 u8 radar_chan;
739 int res = 0;
740
741 /* Our configuration */
742 mode = iface->current_mode;
743 start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1);
744 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
745
746 /* Check we are on DFS channel(s) */
747 if (!dfs_check_chans_radar(iface, start_chan_idx, n_chans))
748 return 0;
749
750 /* Reported via radar event */
751 switch (chan_width) {
752 case CHAN_WIDTH_20_NOHT:
753 case CHAN_WIDTH_20:
754 radar_n_chans = 1;
755 if (frequency == 0)
756 frequency = cf1;
757 break;
758 case CHAN_WIDTH_40:
759 radar_n_chans = 2;
760 frequency = cf1 - 10;
761 break;
762 case CHAN_WIDTH_80:
763 radar_n_chans = 4;
764 frequency = cf1 - 30;
765 break;
766 case CHAN_WIDTH_160:
767 radar_n_chans = 8;
768 frequency = cf1 - 70;
769 break;
770 default:
771 wpa_printf(MSG_INFO, "DFS chan_width %d not supported",
772 chan_width);
773 break;
774 }
775
776 ieee80211_freq_to_chan(frequency, &radar_chan);
777
778 for (i = 0; i < n_chans; i++) {
779 chan = &mode->channels[start_chan_idx + i];
780 if (!(chan->flag & HOSTAPD_CHAN_RADAR))
781 continue;
782 for (j = 0; j < radar_n_chans; j++) {
783 wpa_printf(MSG_DEBUG, "checking our: %d, radar: %d",
784 chan->chan, radar_chan + j * 4);
785 if (chan->chan == radar_chan + j * 4)
786 res++;
787 }
788 }
789
790 wpa_printf(MSG_DEBUG, "overlapped: %d", res);
791
792 return res;
793 }
794
795
dfs_get_cac_time(struct hostapd_iface * iface,int start_chan_idx,int n_chans)796 static unsigned int dfs_get_cac_time(struct hostapd_iface *iface,
797 int start_chan_idx, int n_chans)
798 {
799 struct hostapd_channel_data *channel;
800 struct hostapd_hw_modes *mode;
801 int i;
802 unsigned int cac_time_ms = 0;
803
804 mode = iface->current_mode;
805
806 for (i = 0; i < n_chans; i++) {
807 if (start_chan_idx + i >= mode->num_channels)
808 break;
809 channel = &mode->channels[start_chan_idx + i];
810 if (!(channel->flag & HOSTAPD_CHAN_RADAR))
811 continue;
812 if (channel->dfs_cac_ms > cac_time_ms)
813 cac_time_ms = channel->dfs_cac_ms;
814 }
815
816 return cac_time_ms;
817 }
818
819
820 /*
821 * Main DFS handler
822 * 1 - continue channel/ap setup
823 * 0 - channel/ap setup will be continued after CAC
824 * -1 - hit critical error
825 */
hostapd_handle_dfs(struct hostapd_iface * iface)826 int hostapd_handle_dfs(struct hostapd_iface *iface)
827 {
828 int res, n_chans, n_chans1, start_chan_idx, start_chan_idx1;
829 int skip_radar = 0;
830
831 if (is_6ghz_freq(iface->freq))
832 return 1;
833
834 if (!iface->current_mode) {
835 /*
836 * This can happen with drivers that do not provide mode
837 * information and as such, cannot really use hostapd for DFS.
838 */
839 wpa_printf(MSG_DEBUG,
840 "DFS: No current_mode information - assume no need to perform DFS operations by hostapd");
841 return 1;
842 }
843
844 iface->cac_started = 0;
845
846 do {
847 /* Get start (first) channel for current configuration */
848 start_chan_idx = dfs_get_start_chan_idx(iface,
849 &start_chan_idx1);
850 if (start_chan_idx == -1)
851 return -1;
852
853 /* Get number of used channels, depend on width */
854 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
855
856 /* Setup CAC time */
857 iface->dfs_cac_ms = dfs_get_cac_time(iface, start_chan_idx,
858 n_chans);
859
860 /* Check if any of configured channels require DFS */
861 res = dfs_check_chans_radar(iface, start_chan_idx, n_chans);
862 wpa_printf(MSG_DEBUG,
863 "DFS %d channels required radar detection",
864 res);
865 if (!res)
866 return 1;
867
868 /* Check if all channels are DFS available */
869 res = dfs_check_chans_available(iface, start_chan_idx, n_chans);
870 wpa_printf(MSG_DEBUG,
871 "DFS all channels available, (SKIP CAC): %s",
872 res ? "yes" : "no");
873 if (res)
874 return 1;
875
876 /* Check if any of configured channels is unavailable */
877 res = dfs_check_chans_unavailable(iface, start_chan_idx,
878 n_chans);
879 wpa_printf(MSG_DEBUG, "DFS %d chans unavailable - choose other channel: %s",
880 res, res ? "yes": "no");
881 if (res) {
882 if (dfs_set_valid_channel(iface, skip_radar) < 0) {
883 hostapd_set_state(iface, HAPD_IFACE_DFS);
884 return 0;
885 }
886 }
887 } while (res);
888
889 /* Finally start CAC */
890 hostapd_set_state(iface, HAPD_IFACE_DFS);
891 wpa_printf(MSG_DEBUG, "DFS start CAC on %d MHz%s", iface->freq,
892 dfs_use_radar_background(iface) ? " (background)" : "");
893 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_START
894 "freq=%d chan=%d sec_chan=%d, width=%d, seg0=%d, seg1=%d, cac_time=%ds",
895 iface->freq,
896 iface->conf->channel, iface->conf->secondary_channel,
897 hostapd_get_oper_chwidth(iface->conf),
898 hostapd_get_oper_centr_freq_seg0_idx(iface->conf),
899 hostapd_get_oper_centr_freq_seg1_idx(iface->conf),
900 iface->dfs_cac_ms / 1000);
901
902 res = hostapd_start_dfs_cac(
903 iface, iface->conf->hw_mode, iface->freq, iface->conf->channel,
904 iface->conf->ieee80211n, iface->conf->ieee80211ac,
905 iface->conf->ieee80211ax, iface->conf->ieee80211be,
906 iface->conf->secondary_channel,
907 hostapd_get_oper_chwidth(iface->conf),
908 hostapd_get_oper_centr_freq_seg0_idx(iface->conf),
909 hostapd_get_oper_centr_freq_seg1_idx(iface->conf),
910 dfs_use_radar_background(iface));
911
912 if (res) {
913 wpa_printf(MSG_ERROR, "DFS start_dfs_cac() failed, %d", res);
914 return -1;
915 }
916
917 if (dfs_use_radar_background(iface)) {
918 /* Cache background radar parameters. */
919 iface->radar_background.channel = iface->conf->channel;
920 iface->radar_background.secondary_channel =
921 iface->conf->secondary_channel;
922 iface->radar_background.freq = iface->freq;
923 iface->radar_background.centr_freq_seg0_idx =
924 hostapd_get_oper_centr_freq_seg0_idx(iface->conf);
925 iface->radar_background.centr_freq_seg1_idx =
926 hostapd_get_oper_centr_freq_seg1_idx(iface->conf);
927
928 /*
929 * Let's select a random channel according to the
930 * regulations and perform CAC on dedicated radar chain.
931 */
932 res = dfs_set_valid_channel(iface, 1);
933 if (res < 0)
934 return res;
935
936 iface->radar_background.temp_ch = 1;
937 return 1;
938 }
939
940 return 0;
941 }
942
943
hostapd_is_dfs_chan_available(struct hostapd_iface * iface)944 int hostapd_is_dfs_chan_available(struct hostapd_iface *iface)
945 {
946 int n_chans, n_chans1, start_chan_idx, start_chan_idx1;
947
948 /* Get the start (first) channel for current configuration */
949 start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1);
950 if (start_chan_idx < 0)
951 return 0;
952
953 /* Get the number of used channels, depending on width */
954 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
955
956 /* Check if all channels are DFS available */
957 return dfs_check_chans_available(iface, start_chan_idx, n_chans);
958 }
959
960
hostapd_dfs_request_channel_switch(struct hostapd_iface * iface,int channel,int freq,int secondary_channel,u8 current_vht_oper_chwidth,u8 oper_centr_freq_seg0_idx,u8 oper_centr_freq_seg1_idx)961 static int hostapd_dfs_request_channel_switch(struct hostapd_iface *iface,
962 int channel, int freq,
963 int secondary_channel,
964 u8 current_vht_oper_chwidth,
965 u8 oper_centr_freq_seg0_idx,
966 u8 oper_centr_freq_seg1_idx)
967 {
968 struct hostapd_hw_modes *cmode = iface->current_mode;
969 int ieee80211_mode = IEEE80211_MODE_AP, err;
970 struct csa_settings csa_settings;
971 u8 new_vht_oper_chwidth;
972 unsigned int i;
973
974 wpa_printf(MSG_DEBUG, "DFS will switch to a new channel %d", channel);
975 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NEW_CHANNEL
976 "freq=%d chan=%d sec_chan=%d", freq, channel,
977 secondary_channel);
978
979 new_vht_oper_chwidth = hostapd_get_oper_chwidth(iface->conf);
980 hostapd_set_oper_chwidth(iface->conf, current_vht_oper_chwidth);
981
982 /* Setup CSA request */
983 os_memset(&csa_settings, 0, sizeof(csa_settings));
984 csa_settings.cs_count = 5;
985 csa_settings.block_tx = 1;
986 #ifdef CONFIG_MESH
987 if (iface->mconf)
988 ieee80211_mode = IEEE80211_MODE_MESH;
989 #endif /* CONFIG_MESH */
990 err = hostapd_set_freq_params(&csa_settings.freq_params,
991 iface->conf->hw_mode,
992 freq, channel,
993 iface->conf->enable_edmg,
994 iface->conf->edmg_channel,
995 iface->conf->ieee80211n,
996 iface->conf->ieee80211ac,
997 iface->conf->ieee80211ax,
998 iface->conf->ieee80211be,
999 secondary_channel,
1000 new_vht_oper_chwidth,
1001 oper_centr_freq_seg0_idx,
1002 oper_centr_freq_seg1_idx,
1003 cmode->vht_capab,
1004 &cmode->he_capab[ieee80211_mode],
1005 &cmode->eht_capab[ieee80211_mode]);
1006
1007 if (err) {
1008 wpa_printf(MSG_ERROR,
1009 "DFS failed to calculate CSA freq params");
1010 hostapd_disable_iface(iface);
1011 return err;
1012 }
1013
1014 for (i = 0; i < iface->num_bss; i++) {
1015 err = hostapd_switch_channel(iface->bss[i], &csa_settings);
1016 if (err)
1017 break;
1018 }
1019
1020 if (err) {
1021 wpa_printf(MSG_WARNING,
1022 "DFS failed to schedule CSA (%d) - trying fallback",
1023 err);
1024 iface->freq = freq;
1025 iface->conf->channel = channel;
1026 iface->conf->secondary_channel = secondary_channel;
1027 hostapd_set_oper_chwidth(iface->conf, new_vht_oper_chwidth);
1028 hostapd_set_oper_centr_freq_seg0_idx(iface->conf,
1029 oper_centr_freq_seg0_idx);
1030 hostapd_set_oper_centr_freq_seg1_idx(iface->conf,
1031 oper_centr_freq_seg1_idx);
1032
1033 hostapd_disable_iface(iface);
1034 hostapd_enable_iface(iface);
1035
1036 return 0;
1037 }
1038
1039 /* Channel configuration will be updated once CSA completes and
1040 * ch_switch_notify event is received */
1041 wpa_printf(MSG_DEBUG, "DFS waiting channel switch event");
1042
1043 return 0;
1044 }
1045
1046
hostpad_dfs_update_background_chain(struct hostapd_iface * iface)1047 static void hostpad_dfs_update_background_chain(struct hostapd_iface *iface)
1048 {
1049 int sec = 0;
1050 enum dfs_channel_type channel_type = DFS_NO_CAC_YET;
1051 struct hostapd_channel_data *channel;
1052 u8 oper_centr_freq_seg0_idx = 0;
1053 u8 oper_centr_freq_seg1_idx = 0;
1054
1055 /*
1056 * Allow selection of DFS channel in ETSI to comply with
1057 * uniform spreading.
1058 */
1059 if (iface->dfs_domain == HOSTAPD_DFS_REGION_ETSI)
1060 channel_type = DFS_ANY_CHANNEL;
1061
1062 channel = dfs_get_valid_channel(iface, &sec, &oper_centr_freq_seg0_idx,
1063 &oper_centr_freq_seg1_idx,
1064 channel_type);
1065 if (!channel ||
1066 channel->chan == iface->conf->channel ||
1067 channel->chan == iface->radar_background.channel)
1068 channel = dfs_downgrade_bandwidth(iface, &sec,
1069 &oper_centr_freq_seg0_idx,
1070 &oper_centr_freq_seg1_idx,
1071 &channel_type);
1072 if (!channel ||
1073 hostapd_start_dfs_cac(iface, iface->conf->hw_mode,
1074 channel->freq, channel->chan,
1075 iface->conf->ieee80211n,
1076 iface->conf->ieee80211ac,
1077 iface->conf->ieee80211ax,
1078 iface->conf->ieee80211be,
1079 sec, hostapd_get_oper_chwidth(iface->conf),
1080 oper_centr_freq_seg0_idx,
1081 oper_centr_freq_seg1_idx, true)) {
1082 wpa_printf(MSG_ERROR, "DFS failed to start CAC offchannel");
1083 iface->radar_background.channel = -1;
1084 return;
1085 }
1086
1087 iface->radar_background.channel = channel->chan;
1088 iface->radar_background.freq = channel->freq;
1089 iface->radar_background.secondary_channel = sec;
1090 iface->radar_background.centr_freq_seg0_idx = oper_centr_freq_seg0_idx;
1091 iface->radar_background.centr_freq_seg1_idx = oper_centr_freq_seg1_idx;
1092
1093 wpa_printf(MSG_DEBUG,
1094 "%s: setting background chain to chan %d (%d MHz)",
1095 __func__, channel->chan, channel->freq);
1096 }
1097
1098
1099 static bool
hostapd_dfs_is_background_event(struct hostapd_iface * iface,int freq)1100 hostapd_dfs_is_background_event(struct hostapd_iface *iface, int freq)
1101 {
1102 return dfs_use_radar_background(iface) &&
1103 iface->radar_background.channel != -1 &&
1104 iface->radar_background.freq == freq;
1105 }
1106
1107
1108 static int
hostapd_dfs_start_channel_switch_background(struct hostapd_iface * iface)1109 hostapd_dfs_start_channel_switch_background(struct hostapd_iface *iface)
1110 {
1111 u8 current_vht_oper_chwidth = hostapd_get_oper_chwidth(iface->conf);
1112
1113 iface->conf->channel = iface->radar_background.channel;
1114 iface->freq = iface->radar_background.freq;
1115 iface->conf->secondary_channel =
1116 iface->radar_background.secondary_channel;
1117 hostapd_set_oper_centr_freq_seg0_idx(
1118 iface->conf, iface->radar_background.centr_freq_seg0_idx);
1119 hostapd_set_oper_centr_freq_seg1_idx(
1120 iface->conf, iface->radar_background.centr_freq_seg1_idx);
1121
1122 hostpad_dfs_update_background_chain(iface);
1123
1124 return hostapd_dfs_request_channel_switch(
1125 iface, iface->conf->channel, iface->freq,
1126 iface->conf->secondary_channel, current_vht_oper_chwidth,
1127 hostapd_get_oper_centr_freq_seg0_idx(iface->conf),
1128 hostapd_get_oper_centr_freq_seg1_idx(iface->conf));
1129 }
1130
1131
hostapd_dfs_complete_cac(struct hostapd_iface * iface,int success,int freq,int ht_enabled,int chan_offset,int chan_width,int cf1,int cf2)1132 int hostapd_dfs_complete_cac(struct hostapd_iface *iface, int success, int freq,
1133 int ht_enabled, int chan_offset, int chan_width,
1134 int cf1, int cf2)
1135 {
1136 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_COMPLETED
1137 "success=%d freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
1138 success, freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
1139
1140 if (success) {
1141 /* Complete iface/ap configuration */
1142 if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) {
1143 /* Complete AP configuration for the first bring up. */
1144 if (iface->state != HAPD_IFACE_ENABLED)
1145 hostapd_setup_interface_complete(iface, 0);
1146 else
1147 iface->cac_started = 0;
1148 } else {
1149 set_dfs_state(iface, freq, ht_enabled, chan_offset,
1150 chan_width, cf1, cf2,
1151 HOSTAPD_CHAN_DFS_AVAILABLE);
1152
1153 /*
1154 * Radar event from background chain for the selected
1155 * channel. Perform CSA, move the main chain to the
1156 * selected channel and configure the background chain
1157 * to a new DFS channel.
1158 */
1159 if (hostapd_dfs_is_background_event(iface, freq)) {
1160 iface->radar_background.cac_started = 0;
1161 if (!iface->radar_background.temp_ch)
1162 return 0;
1163
1164 iface->radar_background.temp_ch = 0;
1165 return hostapd_dfs_start_channel_switch_background(iface);
1166 }
1167
1168 /*
1169 * Just mark the channel available when CAC completion
1170 * event is received in enabled state. CAC result could
1171 * have been propagated from another radio having the
1172 * same regulatory configuration. When CAC completion is
1173 * received during non-HAPD_IFACE_ENABLED state, make
1174 * sure the configured channel is available because this
1175 * CAC completion event could have been propagated from
1176 * another radio.
1177 */
1178 if (iface->state != HAPD_IFACE_ENABLED &&
1179 hostapd_is_dfs_chan_available(iface)) {
1180 hostapd_setup_interface_complete(iface, 0);
1181 iface->cac_started = 0;
1182 }
1183 }
1184 } else if (hostapd_dfs_is_background_event(iface, freq)) {
1185 iface->radar_background.cac_started = 0;
1186 hostpad_dfs_update_background_chain(iface);
1187 }
1188
1189 return 0;
1190 }
1191
1192
hostapd_dfs_pre_cac_expired(struct hostapd_iface * iface,int freq,int ht_enabled,int chan_offset,int chan_width,int cf1,int cf2)1193 int hostapd_dfs_pre_cac_expired(struct hostapd_iface *iface, int freq,
1194 int ht_enabled, int chan_offset, int chan_width,
1195 int cf1, int cf2)
1196 {
1197 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_PRE_CAC_EXPIRED
1198 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
1199 freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
1200
1201 /* Proceed only if DFS is not offloaded to the driver */
1202 if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)
1203 return 0;
1204
1205 set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
1206 cf1, cf2, HOSTAPD_CHAN_DFS_USABLE);
1207
1208 return 0;
1209 }
1210
1211
1212 static struct hostapd_channel_data *
dfs_downgrade_bandwidth(struct hostapd_iface * iface,int * secondary_channel,u8 * oper_centr_freq_seg0_idx,u8 * oper_centr_freq_seg1_idx,enum dfs_channel_type * channel_type)1213 dfs_downgrade_bandwidth(struct hostapd_iface *iface, int *secondary_channel,
1214 u8 *oper_centr_freq_seg0_idx,
1215 u8 *oper_centr_freq_seg1_idx,
1216 enum dfs_channel_type *channel_type)
1217 {
1218 struct hostapd_channel_data *channel;
1219
1220 for (;;) {
1221 channel = dfs_get_valid_channel(iface, secondary_channel,
1222 oper_centr_freq_seg0_idx,
1223 oper_centr_freq_seg1_idx,
1224 *channel_type);
1225 if (channel) {
1226 wpa_printf(MSG_DEBUG, "DFS: Selected channel: %d",
1227 channel->chan);
1228 return channel;
1229 }
1230
1231 if (*channel_type != DFS_ANY_CHANNEL) {
1232 *channel_type = DFS_ANY_CHANNEL;
1233 } else {
1234 int oper_chwidth;
1235
1236 oper_chwidth = hostapd_get_oper_chwidth(iface->conf);
1237 if (oper_chwidth == CONF_OPER_CHWIDTH_USE_HT)
1238 break;
1239 *channel_type = DFS_AVAILABLE;
1240 hostapd_set_oper_chwidth(iface->conf, oper_chwidth - 1);
1241 }
1242 }
1243
1244 wpa_printf(MSG_INFO,
1245 "%s: no DFS channels left, waiting for NOP to finish",
1246 __func__);
1247 return NULL;
1248 }
1249
1250
hostapd_dfs_start_channel_switch_cac(struct hostapd_iface * iface)1251 static int hostapd_dfs_start_channel_switch_cac(struct hostapd_iface *iface)
1252 {
1253 struct hostapd_channel_data *channel;
1254 int secondary_channel;
1255 u8 oper_centr_freq_seg0_idx = 0;
1256 u8 oper_centr_freq_seg1_idx = 0;
1257 enum dfs_channel_type channel_type = DFS_ANY_CHANNEL;
1258 int err = 1;
1259
1260 /* Radar detected during active CAC */
1261 iface->cac_started = 0;
1262 channel = dfs_get_valid_channel(iface, &secondary_channel,
1263 &oper_centr_freq_seg0_idx,
1264 &oper_centr_freq_seg1_idx,
1265 channel_type);
1266
1267 if (!channel) {
1268 channel = dfs_downgrade_bandwidth(iface, &secondary_channel,
1269 &oper_centr_freq_seg0_idx,
1270 &oper_centr_freq_seg1_idx,
1271 &channel_type);
1272 if (!channel) {
1273 wpa_printf(MSG_ERROR, "No valid channel available");
1274 return err;
1275 }
1276 }
1277
1278 wpa_printf(MSG_DEBUG, "DFS will switch to a new channel %d",
1279 channel->chan);
1280 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NEW_CHANNEL
1281 "freq=%d chan=%d sec_chan=%d", channel->freq,
1282 channel->chan, secondary_channel);
1283
1284 iface->freq = channel->freq;
1285 iface->conf->channel = channel->chan;
1286 iface->conf->secondary_channel = secondary_channel;
1287 hostapd_set_oper_centr_freq_seg0_idx(iface->conf,
1288 oper_centr_freq_seg0_idx);
1289 hostapd_set_oper_centr_freq_seg1_idx(iface->conf,
1290 oper_centr_freq_seg1_idx);
1291 err = 0;
1292
1293 hostapd_setup_interface_complete(iface, err);
1294 return err;
1295 }
1296
1297
1298 static int
hostapd_dfs_background_start_channel_switch(struct hostapd_iface * iface,int freq)1299 hostapd_dfs_background_start_channel_switch(struct hostapd_iface *iface,
1300 int freq)
1301 {
1302 if (!dfs_use_radar_background(iface))
1303 return -1; /* Background radar chain not supported. */
1304
1305 wpa_printf(MSG_DEBUG,
1306 "%s called (background CAC active: %s, CSA active: %s)",
1307 __func__, iface->radar_background.cac_started ? "yes" : "no",
1308 hostapd_csa_in_progress(iface) ? "yes" : "no");
1309
1310 /* Check if CSA in progress */
1311 if (hostapd_csa_in_progress(iface))
1312 return 0;
1313
1314 if (hostapd_dfs_is_background_event(iface, freq)) {
1315 /*
1316 * Radar pattern is reported on the background chain.
1317 * Just select a new random channel according to the
1318 * regulations for monitoring.
1319 */
1320 hostpad_dfs_update_background_chain(iface);
1321 return 0;
1322 }
1323
1324 /*
1325 * If background radar detection is supported and the radar channel
1326 * monitored by the background chain is available switch to it without
1327 * waiting for the CAC.
1328 */
1329 if (iface->radar_background.channel == -1)
1330 return -1; /* Background radar chain not available. */
1331
1332 if (iface->radar_background.cac_started) {
1333 /*
1334 * Background channel not available yet. Perform CAC on the
1335 * main chain.
1336 */
1337 iface->radar_background.temp_ch = 1;
1338 return -1;
1339 }
1340
1341 return hostapd_dfs_start_channel_switch_background(iface);
1342 }
1343
1344
hostapd_dfs_start_channel_switch(struct hostapd_iface * iface)1345 static int hostapd_dfs_start_channel_switch(struct hostapd_iface *iface)
1346 {
1347 struct hostapd_channel_data *channel;
1348 int secondary_channel;
1349 u8 oper_centr_freq_seg0_idx;
1350 u8 oper_centr_freq_seg1_idx;
1351 enum dfs_channel_type channel_type = DFS_AVAILABLE;
1352 u8 current_vht_oper_chwidth = hostapd_get_oper_chwidth(iface->conf);
1353
1354 wpa_printf(MSG_DEBUG, "%s called (CAC active: %s, CSA active: %s)",
1355 __func__, iface->cac_started ? "yes" : "no",
1356 hostapd_csa_in_progress(iface) ? "yes" : "no");
1357
1358 /* Check if CSA in progress */
1359 if (hostapd_csa_in_progress(iface))
1360 return 0;
1361
1362 /* Check if active CAC */
1363 if (iface->cac_started)
1364 return hostapd_dfs_start_channel_switch_cac(iface);
1365
1366 /*
1367 * Allow selection of DFS channel in ETSI to comply with
1368 * uniform spreading.
1369 */
1370 if (iface->dfs_domain == HOSTAPD_DFS_REGION_ETSI)
1371 channel_type = DFS_ANY_CHANNEL;
1372
1373 /* Perform channel switch/CSA */
1374 channel = dfs_get_valid_channel(iface, &secondary_channel,
1375 &oper_centr_freq_seg0_idx,
1376 &oper_centr_freq_seg1_idx,
1377 channel_type);
1378
1379 if (!channel) {
1380 /*
1381 * If there is no channel to switch immediately to, check if
1382 * there is another channel where we can switch even if it
1383 * requires to perform a CAC first.
1384 */
1385 channel_type = DFS_ANY_CHANNEL;
1386 channel = dfs_downgrade_bandwidth(iface, &secondary_channel,
1387 &oper_centr_freq_seg0_idx,
1388 &oper_centr_freq_seg1_idx,
1389 &channel_type);
1390 if (!channel) {
1391 /*
1392 * Toggle interface state to enter DFS state
1393 * until NOP is finished.
1394 */
1395 hostapd_disable_iface(iface);
1396 hostapd_enable_iface(iface);
1397 return 0;
1398 }
1399
1400 if (channel_type == DFS_ANY_CHANNEL) {
1401 iface->freq = channel->freq;
1402 iface->conf->channel = channel->chan;
1403 iface->conf->secondary_channel = secondary_channel;
1404 hostapd_set_oper_centr_freq_seg0_idx(
1405 iface->conf, oper_centr_freq_seg0_idx);
1406 hostapd_set_oper_centr_freq_seg1_idx(
1407 iface->conf, oper_centr_freq_seg1_idx);
1408
1409 hostapd_disable_iface(iface);
1410 hostapd_enable_iface(iface);
1411 return 0;
1412 }
1413 }
1414
1415 return hostapd_dfs_request_channel_switch(iface, channel->chan,
1416 channel->freq,
1417 secondary_channel,
1418 current_vht_oper_chwidth,
1419 oper_centr_freq_seg0_idx,
1420 oper_centr_freq_seg1_idx);
1421 }
1422
1423
hostapd_dfs_radar_detected(struct hostapd_iface * iface,int freq,int ht_enabled,int chan_offset,int chan_width,int cf1,int cf2)1424 int hostapd_dfs_radar_detected(struct hostapd_iface *iface, int freq,
1425 int ht_enabled, int chan_offset, int chan_width,
1426 int cf1, int cf2)
1427 {
1428 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_RADAR_DETECTED
1429 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
1430 freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
1431
1432 /* Proceed only if DFS is not offloaded to the driver */
1433 if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)
1434 return 0;
1435
1436 if (!iface->conf->ieee80211h)
1437 return 0;
1438
1439 /* mark radar frequency as invalid */
1440 if (!set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
1441 cf1, cf2, HOSTAPD_CHAN_DFS_UNAVAILABLE))
1442 return 0;
1443
1444 if (!hostapd_dfs_is_background_event(iface, freq)) {
1445 /* Skip if reported radar event not overlapped our channels */
1446 if (!dfs_are_channels_overlapped(iface, freq, chan_width,
1447 cf1, cf2))
1448 return 0;
1449 }
1450
1451 if (hostapd_dfs_background_start_channel_switch(iface, freq)) {
1452 /* Radar detected while operating, switch the channel. */
1453 return hostapd_dfs_start_channel_switch(iface);
1454 }
1455
1456 return 0;
1457 }
1458
1459
hostapd_dfs_nop_finished(struct hostapd_iface * iface,int freq,int ht_enabled,int chan_offset,int chan_width,int cf1,int cf2)1460 int hostapd_dfs_nop_finished(struct hostapd_iface *iface, int freq,
1461 int ht_enabled, int chan_offset, int chan_width,
1462 int cf1, int cf2)
1463 {
1464 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NOP_FINISHED
1465 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
1466 freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
1467
1468 /* Proceed only if DFS is not offloaded to the driver */
1469 if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)
1470 return 0;
1471
1472 /* TODO add correct implementation here */
1473 set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
1474 cf1, cf2, HOSTAPD_CHAN_DFS_USABLE);
1475
1476 if (iface->state == HAPD_IFACE_DFS && !iface->cac_started) {
1477 /* Handle cases where all channels were initially unavailable */
1478 hostapd_handle_dfs(iface);
1479 } else if (dfs_use_radar_background(iface) &&
1480 iface->radar_background.channel == -1) {
1481 /* Reset radar background chain if disabled */
1482 hostpad_dfs_update_background_chain(iface);
1483 }
1484
1485 return 0;
1486 }
1487
1488
hostapd_is_dfs_required(struct hostapd_iface * iface)1489 int hostapd_is_dfs_required(struct hostapd_iface *iface)
1490 {
1491 int n_chans, n_chans1, start_chan_idx, start_chan_idx1, res;
1492
1493 if ((!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
1494 !iface->conf->ieee80211h) ||
1495 !iface->current_mode ||
1496 iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A)
1497 return 0;
1498
1499 /* Get start (first) channel for current configuration */
1500 start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1);
1501 if (start_chan_idx == -1)
1502 return -1;
1503
1504 /* Get number of used channels, depend on width */
1505 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
1506
1507 /* Check if any of configured channels require DFS */
1508 res = dfs_check_chans_radar(iface, start_chan_idx, n_chans);
1509 if (res)
1510 return res;
1511 if (start_chan_idx1 >= 0 && n_chans1 > 0)
1512 res = dfs_check_chans_radar(iface, start_chan_idx1, n_chans1);
1513 return res;
1514 }
1515
1516
hostapd_dfs_start_cac(struct hostapd_iface * iface,int freq,int ht_enabled,int chan_offset,int chan_width,int cf1,int cf2)1517 int hostapd_dfs_start_cac(struct hostapd_iface *iface, int freq,
1518 int ht_enabled, int chan_offset, int chan_width,
1519 int cf1, int cf2)
1520 {
1521 if (hostapd_dfs_is_background_event(iface, freq)) {
1522 iface->radar_background.cac_started = 1;
1523 } else {
1524 /* This is called when the driver indicates that an offloaded
1525 * DFS has started CAC. */
1526 hostapd_set_state(iface, HAPD_IFACE_DFS);
1527 iface->cac_started = 1;
1528 }
1529 /* TODO: How to check CAC time for ETSI weather channels? */
1530 iface->dfs_cac_ms = 60000;
1531 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_START
1532 "freq=%d chan=%d chan_offset=%d width=%d seg0=%d "
1533 "seg1=%d cac_time=%ds%s",
1534 freq, (freq - 5000) / 5, chan_offset, chan_width, cf1, cf2,
1535 iface->dfs_cac_ms / 1000,
1536 hostapd_dfs_is_background_event(iface, freq) ?
1537 " (background)" : "");
1538
1539 os_get_reltime(&iface->dfs_cac_start);
1540 return 0;
1541 }
1542
1543
1544 /*
1545 * Main DFS handler for offloaded case.
1546 * 2 - continue channel/AP setup for non-DFS channel
1547 * 1 - continue channel/AP setup for DFS channel
1548 * 0 - channel/AP setup will be continued after CAC
1549 * -1 - hit critical error
1550 */
hostapd_handle_dfs_offload(struct hostapd_iface * iface)1551 int hostapd_handle_dfs_offload(struct hostapd_iface *iface)
1552 {
1553 int dfs_res;
1554
1555 wpa_printf(MSG_DEBUG, "%s: iface->cac_started: %d",
1556 __func__, iface->cac_started);
1557
1558 /*
1559 * If DFS has already been started, then we are being called from a
1560 * callback to continue AP/channel setup. Reset the CAC start flag and
1561 * return.
1562 */
1563 if (iface->cac_started) {
1564 wpa_printf(MSG_DEBUG, "%s: iface->cac_started: %d",
1565 __func__, iface->cac_started);
1566 iface->cac_started = 0;
1567 return 1;
1568 }
1569
1570 dfs_res = hostapd_is_dfs_required(iface);
1571 if (dfs_res > 0) {
1572 wpa_printf(MSG_DEBUG,
1573 "%s: freq %d MHz requires DFS for %d chans",
1574 __func__, iface->freq, dfs_res);
1575 return 0;
1576 }
1577
1578 wpa_printf(MSG_DEBUG,
1579 "%s: freq %d MHz does not require DFS. Continue channel/AP setup",
1580 __func__, iface->freq);
1581 return 2;
1582 }
1583
1584
hostapd_is_dfs_overlap(struct hostapd_iface * iface,enum chan_width width,int center_freq)1585 int hostapd_is_dfs_overlap(struct hostapd_iface *iface, enum chan_width width,
1586 int center_freq)
1587 {
1588 struct hostapd_channel_data *chan;
1589 struct hostapd_hw_modes *mode = iface->current_mode;
1590 int half_width;
1591 int res = 0;
1592 int i;
1593
1594 if (!iface->conf->ieee80211h || !mode ||
1595 mode->mode != HOSTAPD_MODE_IEEE80211A)
1596 return 0;
1597
1598 switch (width) {
1599 case CHAN_WIDTH_20_NOHT:
1600 case CHAN_WIDTH_20:
1601 half_width = 10;
1602 break;
1603 case CHAN_WIDTH_40:
1604 half_width = 20;
1605 break;
1606 case CHAN_WIDTH_80:
1607 case CHAN_WIDTH_80P80:
1608 half_width = 40;
1609 break;
1610 case CHAN_WIDTH_160:
1611 half_width = 80;
1612 break;
1613 default:
1614 wpa_printf(MSG_WARNING, "DFS chanwidth %d not supported",
1615 width);
1616 return 0;
1617 }
1618
1619 for (i = 0; i < mode->num_channels; i++) {
1620 chan = &mode->channels[i];
1621
1622 if (!(chan->flag & HOSTAPD_CHAN_RADAR))
1623 continue;
1624
1625 if ((chan->flag & HOSTAPD_CHAN_DFS_MASK) ==
1626 HOSTAPD_CHAN_DFS_AVAILABLE)
1627 continue;
1628
1629 if (center_freq - chan->freq < half_width &&
1630 chan->freq - center_freq < half_width)
1631 res++;
1632 }
1633
1634 wpa_printf(MSG_DEBUG, "DFS CAC required: (%d, %d): in range: %s",
1635 center_freq - half_width, center_freq + half_width,
1636 res ? "yes" : "no");
1637
1638 return res;
1639 }
1640