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