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