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