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