• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_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 */
366 	if (iface->conf->ieee80211ac || iface->conf->ieee80211ax) {
367 		switch (hostapd_get_oper_chwidth(iface->conf)) {
368 		case CONF_OPER_CHWIDTH_USE_HT:
369 			break;
370 		case CONF_OPER_CHWIDTH_80MHZ:
371 			channel_no = hostapd_get_oper_centr_freq_seg0_idx(
372 				iface->conf) - 6;
373 			break;
374 		case CONF_OPER_CHWIDTH_160MHZ:
375 			channel_no = hostapd_get_oper_centr_freq_seg0_idx(
376 				iface->conf) - 14;
377 			break;
378 		case CONF_OPER_CHWIDTH_80P80MHZ:
379 			channel_no = hostapd_get_oper_centr_freq_seg0_idx(
380 				iface->conf) - 6;
381 			chan_seg1 = hostapd_get_oper_centr_freq_seg1_idx(
382 				iface->conf) - 6;
383 			break;
384 		default:
385 			wpa_printf(MSG_INFO,
386 				   "DFS only VHT20/40/80/160/80+80 is supported now");
387 			channel_no = -1;
388 			break;
389 		}
390 	}
391 
392 	/* Get idx */
393 	mode = iface->current_mode;
394 	for (i = 0; i < mode->num_channels; i++) {
395 		chan = &mode->channels[i];
396 		if (chan->chan == channel_no) {
397 			res = i;
398 			break;
399 		}
400 	}
401 
402 	if (res != -1 && chan_seg1 > -1) {
403 		int found = 0;
404 
405 		/* Get idx for seg1 */
406 		mode = iface->current_mode;
407 		for (i = 0; i < mode->num_channels; i++) {
408 			chan = &mode->channels[i];
409 			if (chan->chan == chan_seg1) {
410 				*seg1_start = i;
411 				found = 1;
412 				break;
413 			}
414 		}
415 		if (!found)
416 			res = -1;
417 	}
418 
419 	if (res == -1) {
420 		wpa_printf(MSG_DEBUG,
421 			   "DFS chan_idx seems wrong; num-ch: %d ch-no: %d conf-ch-no: %d 11n: %d sec-ch: %d vht-oper-width: %d",
422 			   mode->num_channels, channel_no, iface->conf->channel,
423 			   iface->conf->ieee80211n,
424 			   iface->conf->secondary_channel,
425 			   hostapd_get_oper_chwidth(iface->conf));
426 
427 		for (i = 0; i < mode->num_channels; i++) {
428 			wpa_printf(MSG_DEBUG, "Available channel: %d",
429 				   mode->channels[i].chan);
430 		}
431 	}
432 
433 	return res;
434 }
435 
436 
437 /* At least one channel have radar flag */
dfs_check_chans_radar(struct hostapd_iface * iface,int start_chan_idx,int n_chans)438 static int dfs_check_chans_radar(struct hostapd_iface *iface,
439 				 int start_chan_idx, int n_chans)
440 {
441 	struct hostapd_channel_data *channel;
442 	struct hostapd_hw_modes *mode;
443 	int i, res = 0;
444 
445 	mode = iface->current_mode;
446 
447 	for (i = 0; i < n_chans; i++) {
448 		if (start_chan_idx + i >= mode->num_channels)
449 			break;
450 		channel = &mode->channels[start_chan_idx + i];
451 		if (channel->flag & HOSTAPD_CHAN_RADAR)
452 			res++;
453 	}
454 
455 	return res;
456 }
457 
458 
459 /* All channels available */
dfs_check_chans_available(struct hostapd_iface * iface,int start_chan_idx,int n_chans)460 static int dfs_check_chans_available(struct hostapd_iface *iface,
461 				     int start_chan_idx, int n_chans)
462 {
463 	struct hostapd_channel_data *channel;
464 	struct hostapd_hw_modes *mode;
465 	int i;
466 
467 	mode = iface->current_mode;
468 
469 	for (i = 0; i < n_chans; i++) {
470 		channel = &mode->channels[start_chan_idx + i];
471 
472 		if (channel->flag & HOSTAPD_CHAN_DISABLED)
473 			break;
474 
475 		if (!(channel->flag & HOSTAPD_CHAN_RADAR))
476 			continue;
477 
478 		if ((channel->flag & HOSTAPD_CHAN_DFS_MASK) !=
479 		    HOSTAPD_CHAN_DFS_AVAILABLE)
480 			break;
481 	}
482 
483 	return i == n_chans;
484 }
485 
486 
487 /* At least one channel unavailable */
dfs_check_chans_unavailable(struct hostapd_iface * iface,int start_chan_idx,int n_chans)488 static int dfs_check_chans_unavailable(struct hostapd_iface *iface,
489 				       int start_chan_idx,
490 				       int n_chans)
491 {
492 	struct hostapd_channel_data *channel;
493 	struct hostapd_hw_modes *mode;
494 	int i, res = 0;
495 
496 	mode = iface->current_mode;
497 
498 	for (i = 0; i < n_chans; i++) {
499 		channel = &mode->channels[start_chan_idx + i];
500 		if (channel->flag & HOSTAPD_CHAN_DISABLED)
501 			res++;
502 		if ((channel->flag & HOSTAPD_CHAN_DFS_MASK) ==
503 		    HOSTAPD_CHAN_DFS_UNAVAILABLE)
504 			res++;
505 	}
506 
507 	return res;
508 }
509 
510 
511 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)512 dfs_get_valid_channel(struct hostapd_iface *iface,
513 		      int *secondary_channel,
514 		      u8 *oper_centr_freq_seg0_idx,
515 		      u8 *oper_centr_freq_seg1_idx,
516 		      enum dfs_channel_type type)
517 {
518 	struct hostapd_hw_modes *mode;
519 	struct hostapd_channel_data *chan = NULL;
520 	struct hostapd_channel_data *chan2 = NULL;
521 	int num_available_chandefs;
522 	int chan_idx, chan_idx2;
523 	int sec_chan_idx_80p80 = -1;
524 	int i;
525 	u32 _rand;
526 
527 	wpa_printf(MSG_DEBUG, "DFS: Selecting random channel");
528 	*secondary_channel = 0;
529 	*oper_centr_freq_seg0_idx = 0;
530 	*oper_centr_freq_seg1_idx = 0;
531 
532 	if (iface->current_mode == NULL)
533 		return NULL;
534 
535 	mode = iface->current_mode;
536 	if (mode->mode != HOSTAPD_MODE_IEEE80211A)
537 		return NULL;
538 
539 	/* Get the count first */
540 	num_available_chandefs = dfs_find_channel(iface, NULL, 0, type);
541 	wpa_printf(MSG_DEBUG, "DFS: num_available_chandefs=%d",
542 		   num_available_chandefs);
543 	if (num_available_chandefs == 0)
544 		return NULL;
545 
546 	if (os_get_random((u8 *) &_rand, sizeof(_rand)) < 0)
547 		return NULL;
548 	chan_idx = _rand % num_available_chandefs;
549 	dfs_find_channel(iface, &chan, chan_idx, type);
550 	if (!chan) {
551 		wpa_printf(MSG_DEBUG, "DFS: no random channel found");
552 		return NULL;
553 	}
554 	wpa_printf(MSG_DEBUG, "DFS: got random channel %d (%d)",
555 		   chan->freq, chan->chan);
556 
557 	/* dfs_find_channel() calculations assume HT40+ */
558 	if (iface->conf->secondary_channel)
559 		*secondary_channel = 1;
560 	else
561 		*secondary_channel = 0;
562 
563 	/* Get secondary channel for HT80P80 */
564 	if (hostapd_get_oper_chwidth(iface->conf) ==
565 	    CONF_OPER_CHWIDTH_80P80MHZ) {
566 		if (num_available_chandefs <= 1) {
567 			wpa_printf(MSG_ERROR,
568 				   "only 1 valid chan, can't support 80+80");
569 			return NULL;
570 		}
571 
572 		/*
573 		 * Loop all channels except channel1 to find a valid channel2
574 		 * that is not adjacent to channel1.
575 		 */
576 		for (i = 0; i < num_available_chandefs - 1; i++) {
577 			/* start from chan_idx + 1, end when chan_idx - 1 */
578 			chan_idx2 = (chan_idx + 1 + i) % num_available_chandefs;
579 			dfs_find_channel(iface, &chan2, chan_idx2, type);
580 			if (chan2 && abs(chan2->chan - chan->chan) > 12) {
581 				/* two channels are not adjacent */
582 				sec_chan_idx_80p80 = chan2->chan;
583 				wpa_printf(MSG_DEBUG,
584 					   "DFS: got second chan: %d (%d)",
585 					   chan2->freq, chan2->chan);
586 				break;
587 			}
588 		}
589 
590 		/* Check if we got a valid secondary channel which is not
591 		 * adjacent to the first channel.
592 		 */
593 		if (sec_chan_idx_80p80 == -1) {
594 			wpa_printf(MSG_INFO,
595 				   "DFS: failed to get chan2 for 80+80");
596 			return NULL;
597 		}
598 	}
599 
600 	dfs_adjust_center_freq(iface, chan,
601 			       *secondary_channel,
602 			       sec_chan_idx_80p80,
603 			       oper_centr_freq_seg0_idx,
604 			       oper_centr_freq_seg1_idx);
605 
606 	return chan;
607 }
608 
609 
dfs_set_valid_channel(struct hostapd_iface * iface,int skip_radar)610 static int dfs_set_valid_channel(struct hostapd_iface *iface, int skip_radar)
611 {
612 	struct hostapd_channel_data *channel;
613 	u8 cf1 = 0, cf2 = 0;
614 	int sec = 0;
615 
616 	channel = dfs_get_valid_channel(iface, &sec, &cf1, &cf2,
617 					skip_radar ? DFS_AVAILABLE :
618 					DFS_ANY_CHANNEL);
619 	if (!channel) {
620 		wpa_printf(MSG_ERROR, "could not get valid channel");
621 		return -1;
622 	}
623 
624 	iface->freq = channel->freq;
625 	iface->conf->channel = channel->chan;
626 	iface->conf->secondary_channel = sec;
627 	hostapd_set_oper_centr_freq_seg0_idx(iface->conf, cf1);
628 	hostapd_set_oper_centr_freq_seg1_idx(iface->conf, cf2);
629 
630 	return 0;
631 }
632 
633 
set_dfs_state_freq(struct hostapd_iface * iface,int freq,u32 state)634 static int set_dfs_state_freq(struct hostapd_iface *iface, int freq, u32 state)
635 {
636 	struct hostapd_hw_modes *mode;
637 	struct hostapd_channel_data *chan = NULL;
638 	int i;
639 
640 	mode = iface->current_mode;
641 	if (mode == NULL)
642 		return 0;
643 
644 	wpa_printf(MSG_DEBUG, "set_dfs_state 0x%X for %d MHz", state, freq);
645 	for (i = 0; i < iface->current_mode->num_channels; i++) {
646 		chan = &iface->current_mode->channels[i];
647 		if (chan->freq == freq) {
648 			if (chan->flag & HOSTAPD_CHAN_RADAR) {
649 				chan->flag &= ~HOSTAPD_CHAN_DFS_MASK;
650 				chan->flag |= state;
651 				return 1; /* Channel found */
652 			}
653 		}
654 	}
655 	wpa_printf(MSG_WARNING, "Can't set DFS state for freq %d MHz", freq);
656 	return 0;
657 }
658 
659 
set_dfs_state(struct hostapd_iface * iface,int freq,int ht_enabled,int chan_offset,int chan_width,int cf1,int cf2,u32 state)660 static int set_dfs_state(struct hostapd_iface *iface, int freq, int ht_enabled,
661 			 int chan_offset, int chan_width, int cf1,
662 			 int cf2, u32 state)
663 {
664 	int n_chans = 1, i;
665 	struct hostapd_hw_modes *mode;
666 	int frequency = freq;
667 	int frequency2 = 0;
668 	int ret = 0;
669 
670 	mode = iface->current_mode;
671 	if (mode == NULL)
672 		return 0;
673 
674 	if (mode->mode != HOSTAPD_MODE_IEEE80211A) {
675 		wpa_printf(MSG_WARNING, "current_mode != IEEE80211A");
676 		return 0;
677 	}
678 
679 	/* Seems cf1 and chan_width is enough here */
680 	switch (chan_width) {
681 	case CHAN_WIDTH_20_NOHT:
682 	case CHAN_WIDTH_20:
683 		n_chans = 1;
684 		if (frequency == 0)
685 			frequency = cf1;
686 		break;
687 	case CHAN_WIDTH_40:
688 		n_chans = 2;
689 		frequency = cf1 - 10;
690 		break;
691 	case CHAN_WIDTH_80:
692 		n_chans = 4;
693 		frequency = cf1 - 30;
694 		break;
695 	case CHAN_WIDTH_80P80:
696 		n_chans = 4;
697 		frequency = cf1 - 30;
698 		frequency2 = cf2 - 30;
699 		break;
700 	case CHAN_WIDTH_160:
701 		n_chans = 8;
702 		frequency = cf1 - 70;
703 		break;
704 	default:
705 		wpa_printf(MSG_INFO, "DFS chan_width %d not supported",
706 			   chan_width);
707 		break;
708 	}
709 
710 	wpa_printf(MSG_DEBUG, "DFS freq: %dMHz, n_chans: %d", frequency,
711 		   n_chans);
712 	for (i = 0; i < n_chans; i++) {
713 		ret += set_dfs_state_freq(iface, frequency, state);
714 		frequency = frequency + 20;
715 
716 		if (chan_width == CHAN_WIDTH_80P80) {
717 			ret += set_dfs_state_freq(iface, frequency2, state);
718 			frequency2 = frequency2 + 20;
719 		}
720 	}
721 
722 	return ret;
723 }
724 
725 
dfs_are_channels_overlapped(struct hostapd_iface * iface,int freq,int chan_width,int cf1,int cf2)726 static int dfs_are_channels_overlapped(struct hostapd_iface *iface, int freq,
727 				       int chan_width, int cf1, int cf2)
728 {
729 	int start_chan_idx, start_chan_idx1;
730 	struct hostapd_hw_modes *mode;
731 	struct hostapd_channel_data *chan;
732 	int n_chans, n_chans1, i, j, frequency = freq, radar_n_chans = 1;
733 	u8 radar_chan;
734 	int res = 0;
735 
736 	/* Our configuration */
737 	mode = iface->current_mode;
738 	start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1);
739 	n_chans = dfs_get_used_n_chans(iface, &n_chans1);
740 
741 	/* Check we are on DFS channel(s) */
742 	if (!dfs_check_chans_radar(iface, start_chan_idx, n_chans))
743 		return 0;
744 
745 	/* Reported via radar event */
746 	switch (chan_width) {
747 	case CHAN_WIDTH_20_NOHT:
748 	case CHAN_WIDTH_20:
749 		radar_n_chans = 1;
750 		if (frequency == 0)
751 			frequency = cf1;
752 		break;
753 	case CHAN_WIDTH_40:
754 		radar_n_chans = 2;
755 		frequency = cf1 - 10;
756 		break;
757 	case CHAN_WIDTH_80:
758 		radar_n_chans = 4;
759 		frequency = cf1 - 30;
760 		break;
761 	case CHAN_WIDTH_160:
762 		radar_n_chans = 8;
763 		frequency = cf1 - 70;
764 		break;
765 	default:
766 		wpa_printf(MSG_INFO, "DFS chan_width %d not supported",
767 			   chan_width);
768 		break;
769 	}
770 
771 	ieee80211_freq_to_chan(frequency, &radar_chan);
772 
773 	for (i = 0; i < n_chans; i++) {
774 		chan = &mode->channels[start_chan_idx + i];
775 		if (!(chan->flag & HOSTAPD_CHAN_RADAR))
776 			continue;
777 		for (j = 0; j < radar_n_chans; j++) {
778 			wpa_printf(MSG_DEBUG, "checking our: %d, radar: %d",
779 				   chan->chan, radar_chan + j * 4);
780 			if (chan->chan == radar_chan + j * 4)
781 				res++;
782 		}
783 	}
784 
785 	wpa_printf(MSG_DEBUG, "overlapped: %d", res);
786 
787 	return res;
788 }
789 
790 
dfs_get_cac_time(struct hostapd_iface * iface,int start_chan_idx,int n_chans)791 static unsigned int dfs_get_cac_time(struct hostapd_iface *iface,
792 				     int start_chan_idx, int n_chans)
793 {
794 	struct hostapd_channel_data *channel;
795 	struct hostapd_hw_modes *mode;
796 	int i;
797 	unsigned int cac_time_ms = 0;
798 
799 	mode = iface->current_mode;
800 
801 	for (i = 0; i < n_chans; i++) {
802 		if (start_chan_idx + i >= mode->num_channels)
803 			break;
804 		channel = &mode->channels[start_chan_idx + i];
805 		if (!(channel->flag & HOSTAPD_CHAN_RADAR))
806 			continue;
807 		if (channel->dfs_cac_ms > cac_time_ms)
808 			cac_time_ms = channel->dfs_cac_ms;
809 	}
810 
811 	return cac_time_ms;
812 }
813 
814 
815 /*
816  * Main DFS handler
817  * 1 - continue channel/ap setup
818  * 0 - channel/ap setup will be continued after CAC
819  * -1 - hit critical error
820  */
hostapd_handle_dfs(struct hostapd_iface * iface)821 int hostapd_handle_dfs(struct hostapd_iface *iface)
822 {
823 	int res, n_chans, n_chans1, start_chan_idx, start_chan_idx1;
824 	int skip_radar = 0;
825 
826 	if (is_6ghz_freq(iface->freq))
827 		return 1;
828 
829 	if (!iface->current_mode) {
830 		/*
831 		 * This can happen with drivers that do not provide mode
832 		 * information and as such, cannot really use hostapd for DFS.
833 		 */
834 		wpa_printf(MSG_DEBUG,
835 			   "DFS: No current_mode information - assume no need to perform DFS operations by hostapd");
836 		return 1;
837 	}
838 
839 	iface->cac_started = 0;
840 
841 	do {
842 		/* Get start (first) channel for current configuration */
843 		start_chan_idx = dfs_get_start_chan_idx(iface,
844 							&start_chan_idx1);
845 		if (start_chan_idx == -1)
846 			return -1;
847 
848 		/* Get number of used channels, depend on width */
849 		n_chans = dfs_get_used_n_chans(iface, &n_chans1);
850 
851 		/* Setup CAC time */
852 		iface->dfs_cac_ms = dfs_get_cac_time(iface, start_chan_idx,
853 						     n_chans);
854 
855 		/* Check if any of configured channels require DFS */
856 		res = dfs_check_chans_radar(iface, start_chan_idx, n_chans);
857 		wpa_printf(MSG_DEBUG,
858 			   "DFS %d channels required radar detection",
859 			   res);
860 		if (!res)
861 			return 1;
862 
863 		/* Check if all channels are DFS available */
864 		res = dfs_check_chans_available(iface, start_chan_idx, n_chans);
865 		wpa_printf(MSG_DEBUG,
866 			   "DFS all channels available, (SKIP CAC): %s",
867 			   res ? "yes" : "no");
868 		if (res)
869 			return 1;
870 
871 		/* Check if any of configured channels is unavailable */
872 		res = dfs_check_chans_unavailable(iface, start_chan_idx,
873 						  n_chans);
874 		wpa_printf(MSG_DEBUG, "DFS %d chans unavailable - choose other channel: %s",
875 			   res, res ? "yes": "no");
876 		if (res) {
877 			if (dfs_set_valid_channel(iface, skip_radar) < 0) {
878 				hostapd_set_state(iface, HAPD_IFACE_DFS);
879 				return 0;
880 			}
881 		}
882 	} while (res);
883 
884 	/* Finally start CAC */
885 	hostapd_set_state(iface, HAPD_IFACE_DFS);
886 	wpa_printf(MSG_DEBUG, "DFS start CAC on %d MHz%s", iface->freq,
887 		   dfs_use_radar_background(iface) ? " (background)" : "");
888 	wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_START
889 		"freq=%d chan=%d sec_chan=%d, width=%d, seg0=%d, seg1=%d, cac_time=%ds",
890 		iface->freq,
891 		iface->conf->channel, iface->conf->secondary_channel,
892 		hostapd_get_oper_chwidth(iface->conf),
893 		hostapd_get_oper_centr_freq_seg0_idx(iface->conf),
894 		hostapd_get_oper_centr_freq_seg1_idx(iface->conf),
895 		iface->dfs_cac_ms / 1000);
896 
897 	res = hostapd_start_dfs_cac(
898 		iface, iface->conf->hw_mode, iface->freq, iface->conf->channel,
899 		iface->conf->ieee80211n, iface->conf->ieee80211ac,
900 		iface->conf->ieee80211ax, iface->conf->ieee80211be,
901 		iface->conf->secondary_channel,
902 		hostapd_get_oper_chwidth(iface->conf),
903 		hostapd_get_oper_centr_freq_seg0_idx(iface->conf),
904 		hostapd_get_oper_centr_freq_seg1_idx(iface->conf),
905 		dfs_use_radar_background(iface));
906 
907 	if (res) {
908 		wpa_printf(MSG_ERROR, "DFS start_dfs_cac() failed, %d", res);
909 		return -1;
910 	}
911 
912 	if (dfs_use_radar_background(iface)) {
913 		/* Cache background radar parameters. */
914 		iface->radar_background.channel = iface->conf->channel;
915 		iface->radar_background.secondary_channel =
916 			iface->conf->secondary_channel;
917 		iface->radar_background.freq = iface->freq;
918 		iface->radar_background.centr_freq_seg0_idx =
919 			hostapd_get_oper_centr_freq_seg0_idx(iface->conf);
920 		iface->radar_background.centr_freq_seg1_idx =
921 			hostapd_get_oper_centr_freq_seg1_idx(iface->conf);
922 
923 		/*
924 		 * Let's select a random channel according to the
925 		 * regulations and perform CAC on dedicated radar chain.
926 		 */
927 		res = dfs_set_valid_channel(iface, 1);
928 		if (res < 0)
929 			return res;
930 
931 		iface->radar_background.temp_ch = 1;
932 		return 1;
933 	}
934 
935 	return 0;
936 }
937 
938 
hostapd_is_dfs_chan_available(struct hostapd_iface * iface)939 int hostapd_is_dfs_chan_available(struct hostapd_iface *iface)
940 {
941 	int n_chans, n_chans1, start_chan_idx, start_chan_idx1;
942 
943 	/* Get the start (first) channel for current configuration */
944 	start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1);
945 	if (start_chan_idx < 0)
946 		return 0;
947 
948 	/* Get the number of used channels, depending on width */
949 	n_chans = dfs_get_used_n_chans(iface, &n_chans1);
950 
951 	/* Check if all channels are DFS available */
952 	return dfs_check_chans_available(iface, start_chan_idx, n_chans);
953 }
954 
955 
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)956 static int hostapd_dfs_request_channel_switch(struct hostapd_iface *iface,
957 					      int channel, int freq,
958 					      int secondary_channel,
959 					      u8 current_vht_oper_chwidth,
960 					      u8 oper_centr_freq_seg0_idx,
961 					      u8 oper_centr_freq_seg1_idx)
962 {
963 	struct hostapd_hw_modes *cmode = iface->current_mode;
964 	int ieee80211_mode = IEEE80211_MODE_AP, err;
965 	struct csa_settings csa_settings;
966 	u8 new_vht_oper_chwidth;
967 	unsigned int i;
968 
969 	wpa_printf(MSG_DEBUG, "DFS will switch to a new channel %d", channel);
970 	wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NEW_CHANNEL
971 		"freq=%d chan=%d sec_chan=%d", freq, channel,
972 		secondary_channel);
973 
974 	new_vht_oper_chwidth = hostapd_get_oper_chwidth(iface->conf);
975 	hostapd_set_oper_chwidth(iface->conf, current_vht_oper_chwidth);
976 
977 	/* Setup CSA request */
978 	os_memset(&csa_settings, 0, sizeof(csa_settings));
979 	csa_settings.cs_count = 5;
980 	csa_settings.block_tx = 1;
981 #ifdef CONFIG_MESH
982 	if (iface->mconf)
983 		ieee80211_mode = IEEE80211_MODE_MESH;
984 #endif /* CONFIG_MESH */
985 	err = hostapd_set_freq_params(&csa_settings.freq_params,
986 				      iface->conf->hw_mode,
987 				      freq, channel,
988 				      iface->conf->enable_edmg,
989 				      iface->conf->edmg_channel,
990 				      iface->conf->ieee80211n,
991 				      iface->conf->ieee80211ac,
992 				      iface->conf->ieee80211ax,
993 				      iface->conf->ieee80211be,
994 				      secondary_channel,
995 				      new_vht_oper_chwidth,
996 				      oper_centr_freq_seg0_idx,
997 				      oper_centr_freq_seg1_idx,
998 				      cmode->vht_capab,
999 				      &cmode->he_capab[ieee80211_mode],
1000 				      &cmode->eht_capab[ieee80211_mode]);
1001 
1002 	if (err) {
1003 		wpa_printf(MSG_ERROR,
1004 			   "DFS failed to calculate CSA freq params");
1005 		hostapd_disable_iface(iface);
1006 		return err;
1007 	}
1008 
1009 	for (i = 0; i < iface->num_bss; i++) {
1010 		err = hostapd_switch_channel(iface->bss[i], &csa_settings);
1011 		if (err)
1012 			break;
1013 	}
1014 
1015 	if (err) {
1016 		wpa_printf(MSG_WARNING,
1017 			   "DFS failed to schedule CSA (%d) - trying fallback",
1018 			   err);
1019 		iface->freq = freq;
1020 		iface->conf->channel = channel;
1021 		iface->conf->secondary_channel = secondary_channel;
1022 		hostapd_set_oper_chwidth(iface->conf, new_vht_oper_chwidth);
1023 		hostapd_set_oper_centr_freq_seg0_idx(iface->conf,
1024 						     oper_centr_freq_seg0_idx);
1025 		hostapd_set_oper_centr_freq_seg1_idx(iface->conf,
1026 						     oper_centr_freq_seg1_idx);
1027 
1028 		hostapd_disable_iface(iface);
1029 		hostapd_enable_iface(iface);
1030 
1031 		return 0;
1032 	}
1033 
1034 	/* Channel configuration will be updated once CSA completes and
1035 	 * ch_switch_notify event is received */
1036 	wpa_printf(MSG_DEBUG, "DFS waiting channel switch event");
1037 
1038 	return 0;
1039 }
1040 
1041 
hostpad_dfs_update_background_chain(struct hostapd_iface * iface)1042 static void hostpad_dfs_update_background_chain(struct hostapd_iface *iface)
1043 {
1044 	int sec = 0;
1045 	enum dfs_channel_type channel_type = DFS_NO_CAC_YET;
1046 	struct hostapd_channel_data *channel;
1047 	u8 oper_centr_freq_seg0_idx = 0;
1048 	u8 oper_centr_freq_seg1_idx = 0;
1049 
1050 	/*
1051 	 * Allow selection of DFS channel in ETSI to comply with
1052 	 * uniform spreading.
1053 	 */
1054 	if (iface->dfs_domain == HOSTAPD_DFS_REGION_ETSI)
1055 		channel_type = DFS_ANY_CHANNEL;
1056 
1057 	channel = dfs_get_valid_channel(iface, &sec, &oper_centr_freq_seg0_idx,
1058 					&oper_centr_freq_seg1_idx,
1059 					channel_type);
1060 	if (!channel ||
1061 	    channel->chan == iface->conf->channel ||
1062 	    channel->chan == iface->radar_background.channel)
1063 		channel = dfs_downgrade_bandwidth(iface, &sec,
1064 						  &oper_centr_freq_seg0_idx,
1065 						  &oper_centr_freq_seg1_idx,
1066 						  &channel_type);
1067 	if (!channel ||
1068 	    hostapd_start_dfs_cac(iface, iface->conf->hw_mode,
1069 				  channel->freq, channel->chan,
1070 				  iface->conf->ieee80211n,
1071 				  iface->conf->ieee80211ac,
1072 				  iface->conf->ieee80211ax,
1073 				  iface->conf->ieee80211be,
1074 				  sec, hostapd_get_oper_chwidth(iface->conf),
1075 				  oper_centr_freq_seg0_idx,
1076 				  oper_centr_freq_seg1_idx, true)) {
1077 		wpa_printf(MSG_ERROR, "DFS failed to start CAC offchannel");
1078 		iface->radar_background.channel = -1;
1079 		return;
1080 	}
1081 
1082 	iface->radar_background.channel = channel->chan;
1083 	iface->radar_background.freq = channel->freq;
1084 	iface->radar_background.secondary_channel = sec;
1085 	iface->radar_background.centr_freq_seg0_idx = oper_centr_freq_seg0_idx;
1086 	iface->radar_background.centr_freq_seg1_idx = oper_centr_freq_seg1_idx;
1087 
1088 	wpa_printf(MSG_DEBUG,
1089 		   "%s: setting background chain to chan %d (%d MHz)",
1090 		   __func__, channel->chan, channel->freq);
1091 }
1092 
1093 
1094 static bool
hostapd_dfs_is_background_event(struct hostapd_iface * iface,int freq)1095 hostapd_dfs_is_background_event(struct hostapd_iface *iface, int freq)
1096 {
1097 	return dfs_use_radar_background(iface) &&
1098 		iface->radar_background.channel != -1 &&
1099 		iface->radar_background.freq == freq;
1100 }
1101 
1102 
1103 static int
hostapd_dfs_start_channel_switch_background(struct hostapd_iface * iface)1104 hostapd_dfs_start_channel_switch_background(struct hostapd_iface *iface)
1105 {
1106 	u8 current_vht_oper_chwidth = hostapd_get_oper_chwidth(iface->conf);
1107 
1108 	iface->conf->channel = iface->radar_background.channel;
1109 	iface->freq = iface->radar_background.freq;
1110 	iface->conf->secondary_channel =
1111 		iface->radar_background.secondary_channel;
1112 	hostapd_set_oper_centr_freq_seg0_idx(
1113 		iface->conf, iface->radar_background.centr_freq_seg0_idx);
1114 	hostapd_set_oper_centr_freq_seg1_idx(
1115 		iface->conf, iface->radar_background.centr_freq_seg1_idx);
1116 
1117 	hostpad_dfs_update_background_chain(iface);
1118 
1119 	return hostapd_dfs_request_channel_switch(
1120 		iface, iface->conf->channel, iface->freq,
1121 		iface->conf->secondary_channel, current_vht_oper_chwidth,
1122 		hostapd_get_oper_centr_freq_seg0_idx(iface->conf),
1123 		hostapd_get_oper_centr_freq_seg1_idx(iface->conf));
1124 }
1125 
1126 
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)1127 int hostapd_dfs_complete_cac(struct hostapd_iface *iface, int success, int freq,
1128 			     int ht_enabled, int chan_offset, int chan_width,
1129 			     int cf1, int cf2)
1130 {
1131 	wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_COMPLETED
1132 		"success=%d freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
1133 		success, freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
1134 
1135 	if (success) {
1136 		/* Complete iface/ap configuration */
1137 		if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) {
1138 			/* Complete AP configuration for the first bring up. */
1139 			if (iface->state != HAPD_IFACE_ENABLED)
1140 				hostapd_setup_interface_complete(iface, 0);
1141 			else
1142 				iface->cac_started = 0;
1143 		} else {
1144 			set_dfs_state(iface, freq, ht_enabled, chan_offset,
1145 				      chan_width, cf1, cf2,
1146 				      HOSTAPD_CHAN_DFS_AVAILABLE);
1147 
1148 			/*
1149 			 * Radar event from background chain for the selected
1150 			 * channel. Perform CSA, move the main chain to the
1151 			 * selected channel and configure the background chain
1152 			 * to a new DFS channel.
1153 			 */
1154 			if (hostapd_dfs_is_background_event(iface, freq)) {
1155 				iface->radar_background.cac_started = 0;
1156 				if (!iface->radar_background.temp_ch)
1157 					return 0;
1158 
1159 				iface->radar_background.temp_ch = 0;
1160 				return hostapd_dfs_start_channel_switch_background(iface);
1161 			}
1162 
1163 			/*
1164 			 * Just mark the channel available when CAC completion
1165 			 * event is received in enabled state. CAC result could
1166 			 * have been propagated from another radio having the
1167 			 * same regulatory configuration. When CAC completion is
1168 			 * received during non-HAPD_IFACE_ENABLED state, make
1169 			 * sure the configured channel is available because this
1170 			 * CAC completion event could have been propagated from
1171 			 * another radio.
1172 			 */
1173 			if (iface->state != HAPD_IFACE_ENABLED &&
1174 			    hostapd_is_dfs_chan_available(iface)) {
1175 				hostapd_setup_interface_complete(iface, 0);
1176 				iface->cac_started = 0;
1177 			}
1178 		}
1179 	} else if (hostapd_dfs_is_background_event(iface, freq)) {
1180 		iface->radar_background.cac_started = 0;
1181 		hostpad_dfs_update_background_chain(iface);
1182 	}
1183 
1184 	return 0;
1185 }
1186 
1187 
hostapd_dfs_pre_cac_expired(struct hostapd_iface * iface,int freq,int ht_enabled,int chan_offset,int chan_width,int cf1,int cf2)1188 int hostapd_dfs_pre_cac_expired(struct hostapd_iface *iface, int freq,
1189 				int ht_enabled, int chan_offset, int chan_width,
1190 				int cf1, int cf2)
1191 {
1192 	wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_PRE_CAC_EXPIRED
1193 		"freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
1194 		freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
1195 
1196 	/* Proceed only if DFS is not offloaded to the driver */
1197 	if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)
1198 		return 0;
1199 
1200 	set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
1201 		      cf1, cf2, HOSTAPD_CHAN_DFS_USABLE);
1202 
1203 	return 0;
1204 }
1205 
1206 
1207 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)1208 dfs_downgrade_bandwidth(struct hostapd_iface *iface, int *secondary_channel,
1209 			u8 *oper_centr_freq_seg0_idx,
1210 			u8 *oper_centr_freq_seg1_idx,
1211 			enum dfs_channel_type *channel_type)
1212 {
1213 	struct hostapd_channel_data *channel;
1214 
1215 	for (;;) {
1216 		channel = dfs_get_valid_channel(iface, secondary_channel,
1217 						oper_centr_freq_seg0_idx,
1218 						oper_centr_freq_seg1_idx,
1219 						*channel_type);
1220 		if (channel) {
1221 			wpa_printf(MSG_DEBUG, "DFS: Selected channel: %d",
1222 				   channel->chan);
1223 			return channel;
1224 		}
1225 
1226 		if (*channel_type != DFS_ANY_CHANNEL) {
1227 			*channel_type = DFS_ANY_CHANNEL;
1228 		} else {
1229 			int oper_chwidth;
1230 
1231 			oper_chwidth = hostapd_get_oper_chwidth(iface->conf);
1232 			if (oper_chwidth == CONF_OPER_CHWIDTH_USE_HT)
1233 				break;
1234 			*channel_type = DFS_AVAILABLE;
1235 			hostapd_set_oper_chwidth(iface->conf, oper_chwidth - 1);
1236 		}
1237 	}
1238 
1239 	wpa_printf(MSG_INFO,
1240 		   "%s: no DFS channels left, waiting for NOP to finish",
1241 		   __func__);
1242 	return NULL;
1243 }
1244 
1245 
hostapd_dfs_start_channel_switch_cac(struct hostapd_iface * iface)1246 static int hostapd_dfs_start_channel_switch_cac(struct hostapd_iface *iface)
1247 {
1248 	struct hostapd_channel_data *channel;
1249 	int secondary_channel;
1250 	u8 oper_centr_freq_seg0_idx = 0;
1251 	u8 oper_centr_freq_seg1_idx = 0;
1252 	enum dfs_channel_type channel_type = DFS_ANY_CHANNEL;
1253 	int err = 1;
1254 
1255 	/* Radar detected during active CAC */
1256 	iface->cac_started = 0;
1257 	channel = dfs_get_valid_channel(iface, &secondary_channel,
1258 					&oper_centr_freq_seg0_idx,
1259 					&oper_centr_freq_seg1_idx,
1260 					channel_type);
1261 
1262 	if (!channel) {
1263 		channel = dfs_downgrade_bandwidth(iface, &secondary_channel,
1264 						  &oper_centr_freq_seg0_idx,
1265 						  &oper_centr_freq_seg1_idx,
1266 						  &channel_type);
1267 		if (!channel) {
1268 			wpa_printf(MSG_ERROR, "No valid channel available");
1269 			return err;
1270 		}
1271 	}
1272 
1273 	wpa_printf(MSG_DEBUG, "DFS will switch to a new channel %d",
1274 		   channel->chan);
1275 	wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NEW_CHANNEL
1276 		"freq=%d chan=%d sec_chan=%d", channel->freq,
1277 		channel->chan, secondary_channel);
1278 
1279 	iface->freq = channel->freq;
1280 	iface->conf->channel = channel->chan;
1281 	iface->conf->secondary_channel = secondary_channel;
1282 	hostapd_set_oper_centr_freq_seg0_idx(iface->conf,
1283 					     oper_centr_freq_seg0_idx);
1284 	hostapd_set_oper_centr_freq_seg1_idx(iface->conf,
1285 					     oper_centr_freq_seg1_idx);
1286 	err = 0;
1287 
1288 	hostapd_setup_interface_complete(iface, err);
1289 	return err;
1290 }
1291 
1292 
1293 static int
hostapd_dfs_background_start_channel_switch(struct hostapd_iface * iface,int freq)1294 hostapd_dfs_background_start_channel_switch(struct hostapd_iface *iface,
1295 					    int freq)
1296 {
1297 	if (!dfs_use_radar_background(iface))
1298 		return -1; /* Background radar chain not supported. */
1299 
1300 	wpa_printf(MSG_DEBUG,
1301 		   "%s called (background CAC active: %s, CSA active: %s)",
1302 		   __func__, iface->radar_background.cac_started ? "yes" : "no",
1303 		   hostapd_csa_in_progress(iface) ? "yes" : "no");
1304 
1305 	/* Check if CSA in progress */
1306 	if (hostapd_csa_in_progress(iface))
1307 		return 0;
1308 
1309 	if (hostapd_dfs_is_background_event(iface, freq)) {
1310 		/*
1311 		 * Radar pattern is reported on the background chain.
1312 		 * Just select a new random channel according to the
1313 		 * regulations for monitoring.
1314 		 */
1315 		hostpad_dfs_update_background_chain(iface);
1316 		return 0;
1317 	}
1318 
1319 	/*
1320 	 * If background radar detection is supported and the radar channel
1321 	 * monitored by the background chain is available switch to it without
1322 	 * waiting for the CAC.
1323 	 */
1324 	if (iface->radar_background.channel == -1)
1325 		return -1; /* Background radar chain not available. */
1326 
1327 	if (iface->radar_background.cac_started) {
1328 		/*
1329 		 * Background channel not available yet. Perform CAC on the
1330 		 * main chain.
1331 		 */
1332 		iface->radar_background.temp_ch = 1;
1333 		return -1;
1334 	}
1335 
1336 	return hostapd_dfs_start_channel_switch_background(iface);
1337 }
1338 
1339 
hostapd_dfs_start_channel_switch(struct hostapd_iface * iface)1340 static int hostapd_dfs_start_channel_switch(struct hostapd_iface *iface)
1341 {
1342 	struct hostapd_channel_data *channel;
1343 	int secondary_channel;
1344 	u8 oper_centr_freq_seg0_idx;
1345 	u8 oper_centr_freq_seg1_idx;
1346 	enum dfs_channel_type channel_type = DFS_AVAILABLE;
1347 	u8 current_vht_oper_chwidth = hostapd_get_oper_chwidth(iface->conf);
1348 
1349 	wpa_printf(MSG_DEBUG, "%s called (CAC active: %s, CSA active: %s)",
1350 		   __func__, iface->cac_started ? "yes" : "no",
1351 		   hostapd_csa_in_progress(iface) ? "yes" : "no");
1352 
1353 	/* Check if CSA in progress */
1354 	if (hostapd_csa_in_progress(iface))
1355 		return 0;
1356 
1357 	/* Check if active CAC */
1358 	if (iface->cac_started)
1359 		return hostapd_dfs_start_channel_switch_cac(iface);
1360 
1361 	/*
1362 	 * Allow selection of DFS channel in ETSI to comply with
1363 	 * uniform spreading.
1364 	 */
1365 	if (iface->dfs_domain == HOSTAPD_DFS_REGION_ETSI)
1366 		channel_type = DFS_ANY_CHANNEL;
1367 
1368 	/* Perform channel switch/CSA */
1369 	channel = dfs_get_valid_channel(iface, &secondary_channel,
1370 					&oper_centr_freq_seg0_idx,
1371 					&oper_centr_freq_seg1_idx,
1372 					channel_type);
1373 
1374 	if (!channel) {
1375 		/*
1376 		 * If there is no channel to switch immediately to, check if
1377 		 * there is another channel where we can switch even if it
1378 		 * requires to perform a CAC first.
1379 		 */
1380 		channel_type = DFS_ANY_CHANNEL;
1381 		channel = dfs_downgrade_bandwidth(iface, &secondary_channel,
1382 						  &oper_centr_freq_seg0_idx,
1383 						  &oper_centr_freq_seg1_idx,
1384 						  &channel_type);
1385 		if (!channel) {
1386 			/*
1387 			 * Toggle interface state to enter DFS state
1388 			 * until NOP is finished.
1389 			 */
1390 			hostapd_disable_iface(iface);
1391 			hostapd_enable_iface(iface);
1392 			return 0;
1393 		}
1394 
1395 		if (channel_type == DFS_ANY_CHANNEL) {
1396 			iface->freq = channel->freq;
1397 			iface->conf->channel = channel->chan;
1398 			iface->conf->secondary_channel = secondary_channel;
1399 			hostapd_set_oper_centr_freq_seg0_idx(
1400 				iface->conf, oper_centr_freq_seg0_idx);
1401 			hostapd_set_oper_centr_freq_seg1_idx(
1402 				iface->conf, oper_centr_freq_seg1_idx);
1403 
1404 			hostapd_disable_iface(iface);
1405 			hostapd_enable_iface(iface);
1406 			return 0;
1407 		}
1408 	}
1409 
1410 	return hostapd_dfs_request_channel_switch(iface, channel->chan,
1411 						  channel->freq,
1412 						  secondary_channel,
1413 						  current_vht_oper_chwidth,
1414 						  oper_centr_freq_seg0_idx,
1415 						  oper_centr_freq_seg1_idx);
1416 }
1417 
1418 
hostapd_dfs_radar_detected(struct hostapd_iface * iface,int freq,int ht_enabled,int chan_offset,int chan_width,int cf1,int cf2)1419 int hostapd_dfs_radar_detected(struct hostapd_iface *iface, int freq,
1420 			       int ht_enabled, int chan_offset, int chan_width,
1421 			       int cf1, int cf2)
1422 {
1423 	wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_RADAR_DETECTED
1424 		"freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
1425 		freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
1426 
1427 	/* Proceed only if DFS is not offloaded to the driver */
1428 	if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)
1429 		return 0;
1430 
1431 	if (!iface->conf->ieee80211h)
1432 		return 0;
1433 
1434 	/* mark radar frequency as invalid */
1435 	if (!set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
1436 			   cf1, cf2, HOSTAPD_CHAN_DFS_UNAVAILABLE))
1437 		return 0;
1438 
1439 	if (!hostapd_dfs_is_background_event(iface, freq)) {
1440 		/* Skip if reported radar event not overlapped our channels */
1441 		if (!dfs_are_channels_overlapped(iface, freq, chan_width,
1442 						 cf1, cf2))
1443 			return 0;
1444 	}
1445 
1446 	if (hostapd_dfs_background_start_channel_switch(iface, freq)) {
1447 		/* Radar detected while operating, switch the channel. */
1448 		return hostapd_dfs_start_channel_switch(iface);
1449 	}
1450 
1451 	return 0;
1452 }
1453 
1454 
hostapd_dfs_nop_finished(struct hostapd_iface * iface,int freq,int ht_enabled,int chan_offset,int chan_width,int cf1,int cf2)1455 int hostapd_dfs_nop_finished(struct hostapd_iface *iface, int freq,
1456 			     int ht_enabled, int chan_offset, int chan_width,
1457 			     int cf1, int cf2)
1458 {
1459 	wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NOP_FINISHED
1460 		"freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
1461 		freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
1462 
1463 	/* Proceed only if DFS is not offloaded to the driver */
1464 	if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)
1465 		return 0;
1466 
1467 	/* TODO add correct implementation here */
1468 	set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
1469 		      cf1, cf2, HOSTAPD_CHAN_DFS_USABLE);
1470 
1471 	if (iface->state == HAPD_IFACE_DFS && !iface->cac_started) {
1472 		/* Handle cases where all channels were initially unavailable */
1473 		hostapd_handle_dfs(iface);
1474 	} else if (dfs_use_radar_background(iface) &&
1475 		   iface->radar_background.channel == -1) {
1476 		/* Reset radar background chain if disabled */
1477 		hostpad_dfs_update_background_chain(iface);
1478 	}
1479 
1480 	return 0;
1481 }
1482 
1483 
hostapd_is_dfs_required(struct hostapd_iface * iface)1484 int hostapd_is_dfs_required(struct hostapd_iface *iface)
1485 {
1486 	int n_chans, n_chans1, start_chan_idx, start_chan_idx1, res;
1487 
1488 	if ((!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
1489 	     !iface->conf->ieee80211h) ||
1490 	    !iface->current_mode ||
1491 	    iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A)
1492 		return 0;
1493 
1494 	/* Get start (first) channel for current configuration */
1495 	start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1);
1496 	if (start_chan_idx == -1)
1497 		return -1;
1498 
1499 	/* Get number of used channels, depend on width */
1500 	n_chans = dfs_get_used_n_chans(iface, &n_chans1);
1501 
1502 	/* Check if any of configured channels require DFS */
1503 	res = dfs_check_chans_radar(iface, start_chan_idx, n_chans);
1504 	if (res)
1505 		return res;
1506 	if (start_chan_idx1 >= 0 && n_chans1 > 0)
1507 		res = dfs_check_chans_radar(iface, start_chan_idx1, n_chans1);
1508 	return res;
1509 }
1510 
1511 
hostapd_dfs_start_cac(struct hostapd_iface * iface,int freq,int ht_enabled,int chan_offset,int chan_width,int cf1,int cf2)1512 int hostapd_dfs_start_cac(struct hostapd_iface *iface, int freq,
1513 			  int ht_enabled, int chan_offset, int chan_width,
1514 			  int cf1, int cf2)
1515 {
1516 	if (hostapd_dfs_is_background_event(iface, freq)) {
1517 		iface->radar_background.cac_started = 1;
1518 	} else {
1519 		/* This is called when the driver indicates that an offloaded
1520 		 * DFS has started CAC. */
1521 		hostapd_set_state(iface, HAPD_IFACE_DFS);
1522 		iface->cac_started = 1;
1523 	}
1524 	/* TODO: How to check CAC time for ETSI weather channels? */
1525 	iface->dfs_cac_ms = 60000;
1526 	wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_START
1527 		"freq=%d chan=%d chan_offset=%d width=%d seg0=%d "
1528 		"seg1=%d cac_time=%ds%s",
1529 		freq, (freq - 5000) / 5, chan_offset, chan_width, cf1, cf2,
1530 		iface->dfs_cac_ms / 1000,
1531 		hostapd_dfs_is_background_event(iface, freq) ?
1532 		" (background)" : "");
1533 
1534 	os_get_reltime(&iface->dfs_cac_start);
1535 	return 0;
1536 }
1537 
1538 
1539 /*
1540  * Main DFS handler for offloaded case.
1541  * 2 - continue channel/AP setup for non-DFS channel
1542  * 1 - continue channel/AP setup for DFS channel
1543  * 0 - channel/AP setup will be continued after CAC
1544  * -1 - hit critical error
1545  */
hostapd_handle_dfs_offload(struct hostapd_iface * iface)1546 int hostapd_handle_dfs_offload(struct hostapd_iface *iface)
1547 {
1548 	int dfs_res;
1549 
1550 	wpa_printf(MSG_DEBUG, "%s: iface->cac_started: %d",
1551 		   __func__, iface->cac_started);
1552 
1553 	/*
1554 	 * If DFS has already been started, then we are being called from a
1555 	 * callback to continue AP/channel setup. Reset the CAC start flag and
1556 	 * return.
1557 	 */
1558 	if (iface->cac_started) {
1559 		wpa_printf(MSG_DEBUG, "%s: iface->cac_started: %d",
1560 			   __func__, iface->cac_started);
1561 		iface->cac_started = 0;
1562 		return 1;
1563 	}
1564 
1565 	dfs_res = hostapd_is_dfs_required(iface);
1566 	if (dfs_res > 0) {
1567 		wpa_printf(MSG_DEBUG,
1568 			   "%s: freq %d MHz requires DFS for %d chans",
1569 			   __func__, iface->freq, dfs_res);
1570 		return 0;
1571 	}
1572 
1573 	wpa_printf(MSG_DEBUG,
1574 		   "%s: freq %d MHz does not require DFS. Continue channel/AP setup",
1575 		   __func__, iface->freq);
1576 	return 2;
1577 }
1578 
1579 
hostapd_is_dfs_overlap(struct hostapd_iface * iface,enum chan_width width,int center_freq)1580 int hostapd_is_dfs_overlap(struct hostapd_iface *iface, enum chan_width width,
1581 			   int center_freq)
1582 {
1583 	struct hostapd_channel_data *chan;
1584 	struct hostapd_hw_modes *mode = iface->current_mode;
1585 	int half_width;
1586 	int res = 0;
1587 	int i;
1588 
1589 	if (!iface->conf->ieee80211h || !mode ||
1590 	    mode->mode != HOSTAPD_MODE_IEEE80211A)
1591 		return 0;
1592 
1593 	switch (width) {
1594 	case CHAN_WIDTH_20_NOHT:
1595 	case CHAN_WIDTH_20:
1596 		half_width = 10;
1597 		break;
1598 	case CHAN_WIDTH_40:
1599 		half_width = 20;
1600 		break;
1601 	case CHAN_WIDTH_80:
1602 	case CHAN_WIDTH_80P80:
1603 		half_width = 40;
1604 		break;
1605 	case CHAN_WIDTH_160:
1606 		half_width = 80;
1607 		break;
1608 	default:
1609 		wpa_printf(MSG_WARNING, "DFS chanwidth %d not supported",
1610 			   width);
1611 		return 0;
1612 	}
1613 
1614 	for (i = 0; i < mode->num_channels; i++) {
1615 		chan = &mode->channels[i];
1616 
1617 		if (!(chan->flag & HOSTAPD_CHAN_RADAR))
1618 			continue;
1619 
1620 		if ((chan->flag & HOSTAPD_CHAN_DFS_MASK) ==
1621 		    HOSTAPD_CHAN_DFS_AVAILABLE)
1622 			continue;
1623 
1624 		if (center_freq - chan->freq < half_width &&
1625 		    chan->freq - center_freq < half_width)
1626 			res++;
1627 	}
1628 
1629 	wpa_printf(MSG_DEBUG, "DFS CAC required: (%d, %d): in range: %s",
1630 		   center_freq - half_width, center_freq + half_width,
1631 		   res ? "yes" : "no");
1632 
1633 	return res;
1634 }
1635