1 /*
2  * ACS - Automatic Channel Selection module
3  * Copyright (c) 2011, Atheros Communications
4  * Copyright (c) 2013, 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 #include <math.h>
12 
13 #include "utils/common.h"
14 #include "utils/list.h"
15 #include "utils/eloop.h"
16 #include "common/ieee802_11_defs.h"
17 #include "common/hw_features_common.h"
18 #include "common/wpa_ctrl.h"
19 #include "drivers/driver.h"
20 #include "hostapd.h"
21 #include "ap_drv_ops.h"
22 #include "ap_config.h"
23 #include "hw_features.h"
24 #include "acs.h"
25 
26 /*
27  * clang complains about implicit conversion from 'double' to 'long double'
28  * just silence the compiler for time being.
29  */
30 #if __clang__
31 #pragma clang diagnostic ignored "-Wdouble-promotion"
32 #endif
33 
34 /*
35  * Automatic Channel Selection
36  * ===========================
37  *
38  * More info at
39  * ------------
40  * http://wireless.kernel.org/en/users/Documentation/acs
41  *
42  * How to use
43  * ----------
44  * - make sure you have CONFIG_ACS=y in hostapd's .config
45  * - use channel=0 or channel=acs to enable ACS
46  *
47  * How does it work
48  * ----------------
49  * 1. passive scans are used to collect survey data
50  *    (it is assumed that scan trigger collection of survey data in driver)
51  * 2. interference factor is calculated for each channel
52  * 3. ideal channel is picked depending on channel width by using adjacent
53  *    channel interference factors
54  *
55  * Known limitations
56  * -----------------
57  * - Current implementation depends heavily on the amount of time willing to
58  *   spend gathering survey data during hostapd startup. Short traffic bursts
59  *   may be missed and a suboptimal channel may be picked.
60  * - Ideal channel may end up overlapping a channel with 40 MHz intolerant BSS
61  *
62  * Todo / Ideas
63  * ------------
64  * - implement other interference computation methods
65  *   - BSS/RSSI based
66  *   - spectral scan based
67  *   (should be possibly to hook this up with current ACS scans)
68  * - add wpa_supplicant support (for P2P)
69  * - collect a histogram of interference over time allowing more educated
70  *   guess about an ideal channel (perhaps CSA could be used to migrate AP to a
71  *   new "better" channel while running)
72  * - include neighboring BSS scan to avoid conflicts with 40 MHz intolerant BSSs
73  *   when choosing the ideal channel
74  *
75  * Survey interference factor implementation details
76  * -------------------------------------------------
77  * Generic interference_factor in struct hostapd_channel_data is used.
78  *
79  * The survey interference factor is defined as the ratio of the
80  * observed busy time over the time we spent on the channel,
81  * this value is then amplified by the observed noise floor on
82  * the channel in comparison to the lowest noise floor observed
83  * on the entire band.
84  *
85  * This corresponds to:
86  * ---
87  * (busy time - tx time) / (active time - tx time) * 2^(chan_nf - band_min_nf)
88  * ---
89  *
90  * The coefficient of 2 reflects the way power in "far-field"
91  * radiation decreases as the square of distance from the antenna [1].
92  * What this does is it decreases the observed busy time ratio if the
93  * noise observed was low but increases it if the noise was high,
94  * proportionally to the way "far field" radiation changes over
95  * distance.
96  *
97  * If channel busy time is not available the fallback is to use channel RX time.
98  *
99  * Since noise floor is in dBm it is necessary to convert it into Watts so that
100  * combined channel interference (e.g., HT40, which uses two channels) can be
101  * calculated easily.
102  * ---
103  * (busy time - tx time) / (active time - tx time) *
104  *    2^(10^(chan_nf/10) - 10^(band_min_nf/10))
105  * ---
106  *
107  * However to account for cases where busy/rx time is 0 (channel load is then
108  * 0%) channel noise floor signal power is combined into the equation so a
109  * channel with lower noise floor is preferred. The equation becomes:
110  * ---
111  * 10^(chan_nf/5) + (busy time - tx time) / (active time - tx time) *
112  *    2^(10^(chan_nf/10) - 10^(band_min_nf/10))
113  * ---
114  *
115  * All this "interference factor" is purely subjective and only time
116  * will tell how usable this is. By using the minimum noise floor we
117  * remove any possible issues due to card calibration. The computation
118  * of the interference factor then is dependent on what the card itself
119  * picks up as the minimum noise, not an actual real possible card
120  * noise value.
121  *
122  * Total interference computation details
123  * --------------------------------------
124  * The above channel interference factor is calculated with no respect to
125  * target operational bandwidth.
126  *
127  * To find an ideal channel the above data is combined by taking into account
128  * the target operational bandwidth and selected band. E.g., on 2.4 GHz channels
129  * overlap with 20 MHz bandwidth, but there is no overlap for 20 MHz bandwidth
130  * on 5 GHz.
131  *
132  * Each valid and possible channel spec (i.e., channel + width) is taken and its
133  * interference factor is computed by summing up interferences of each channel
134  * it overlaps. The one with least total interference is picked up.
135  *
136  * Note: This implies base channel interference factor must be non-negative
137  * allowing easy summing up.
138  *
139  * Example ACS analysis printout
140  * -----------------------------
141  *
142  * ACS: Trying survey-based ACS
143  * ACS: Survey analysis for channel 1 (2412 MHz)
144  * ACS:  1: min_nf=-113 interference_factor=0.0802469 nf=-113 time=162 busy=0 rx=13
145  * ACS:  2: min_nf=-113 interference_factor=0.0745342 nf=-113 time=161 busy=0 rx=12
146  * ACS:  3: min_nf=-113 interference_factor=0.0679012 nf=-113 time=162 busy=0 rx=11
147  * ACS:  4: min_nf=-113 interference_factor=0.0310559 nf=-113 time=161 busy=0 rx=5
148  * ACS:  5: min_nf=-113 interference_factor=0.0248447 nf=-113 time=161 busy=0 rx=4
149  * ACS:  * interference factor average: 0.0557166
150  * ACS: Survey analysis for channel 2 (2417 MHz)
151  * ACS:  1: min_nf=-113 interference_factor=0.0185185 nf=-113 time=162 busy=0 rx=3
152  * ACS:  2: min_nf=-113 interference_factor=0.0246914 nf=-113 time=162 busy=0 rx=4
153  * ACS:  3: min_nf=-113 interference_factor=0.037037 nf=-113 time=162 busy=0 rx=6
154  * ACS:  4: min_nf=-113 interference_factor=0.149068 nf=-113 time=161 busy=0 rx=24
155  * ACS:  5: min_nf=-113 interference_factor=0.0248447 nf=-113 time=161 busy=0 rx=4
156  * ACS:  * interference factor average: 0.050832
157  * ACS: Survey analysis for channel 3 (2422 MHz)
158  * ACS:  1: min_nf=-113 interference_factor=2.51189e-23 nf=-113 time=162 busy=0 rx=0
159  * ACS:  2: min_nf=-113 interference_factor=0.0185185 nf=-113 time=162 busy=0 rx=3
160  * ACS:  3: min_nf=-113 interference_factor=0.0186335 nf=-113 time=161 busy=0 rx=3
161  * ACS:  4: min_nf=-113 interference_factor=0.0186335 nf=-113 time=161 busy=0 rx=3
162  * ACS:  5: min_nf=-113 interference_factor=0.0186335 nf=-113 time=161 busy=0 rx=3
163  * ACS:  * interference factor average: 0.0148838
164  * ACS: Survey analysis for channel 4 (2427 MHz)
165  * ACS:  1: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0
166  * ACS:  2: min_nf=-114 interference_factor=0.0555556 nf=-114 time=162 busy=0 rx=9
167  * ACS:  3: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=161 busy=0 rx=0
168  * ACS:  4: min_nf=-114 interference_factor=0.0186335 nf=-114 time=161 busy=0 rx=3
169  * ACS:  5: min_nf=-114 interference_factor=0.00621118 nf=-114 time=161 busy=0 rx=1
170  * ACS:  * interference factor average: 0.0160801
171  * ACS: Survey analysis for channel 5 (2432 MHz)
172  * ACS:  1: min_nf=-114 interference_factor=0.409938 nf=-113 time=161 busy=0 rx=66
173  * ACS:  2: min_nf=-114 interference_factor=0.0432099 nf=-113 time=162 busy=0 rx=7
174  * ACS:  3: min_nf=-114 interference_factor=0.0124224 nf=-113 time=161 busy=0 rx=2
175  * ACS:  4: min_nf=-114 interference_factor=0.677019 nf=-113 time=161 busy=0 rx=109
176  * ACS:  5: min_nf=-114 interference_factor=0.0186335 nf=-114 time=161 busy=0 rx=3
177  * ACS:  * interference factor average: 0.232244
178  * ACS: Survey analysis for channel 6 (2437 MHz)
179  * ACS:  1: min_nf=-113 interference_factor=0.552795 nf=-113 time=161 busy=0 rx=89
180  * ACS:  2: min_nf=-113 interference_factor=0.0807453 nf=-112 time=161 busy=0 rx=13
181  * ACS:  3: min_nf=-113 interference_factor=0.0310559 nf=-113 time=161 busy=0 rx=5
182  * ACS:  4: min_nf=-113 interference_factor=0.434783 nf=-112 time=161 busy=0 rx=70
183  * ACS:  5: min_nf=-113 interference_factor=0.0621118 nf=-113 time=161 busy=0 rx=10
184  * ACS:  * interference factor average: 0.232298
185  * ACS: Survey analysis for channel 7 (2442 MHz)
186  * ACS:  1: min_nf=-113 interference_factor=0.440994 nf=-112 time=161 busy=0 rx=71
187  * ACS:  2: min_nf=-113 interference_factor=0.385093 nf=-113 time=161 busy=0 rx=62
188  * ACS:  3: min_nf=-113 interference_factor=0.0372671 nf=-113 time=161 busy=0 rx=6
189  * ACS:  4: min_nf=-113 interference_factor=0.0372671 nf=-113 time=161 busy=0 rx=6
190  * ACS:  5: min_nf=-113 interference_factor=0.0745342 nf=-113 time=161 busy=0 rx=12
191  * ACS:  * interference factor average: 0.195031
192  * ACS: Survey analysis for channel 8 (2447 MHz)
193  * ACS:  1: min_nf=-114 interference_factor=0.0496894 nf=-112 time=161 busy=0 rx=8
194  * ACS:  2: min_nf=-114 interference_factor=0.0496894 nf=-114 time=161 busy=0 rx=8
195  * ACS:  3: min_nf=-114 interference_factor=0.0372671 nf=-113 time=161 busy=0 rx=6
196  * ACS:  4: min_nf=-114 interference_factor=0.12963 nf=-113 time=162 busy=0 rx=21
197  * ACS:  5: min_nf=-114 interference_factor=0.166667 nf=-114 time=162 busy=0 rx=27
198  * ACS:  * interference factor average: 0.0865885
199  * ACS: Survey analysis for channel 9 (2452 MHz)
200  * ACS:  1: min_nf=-114 interference_factor=0.0124224 nf=-114 time=161 busy=0 rx=2
201  * ACS:  2: min_nf=-114 interference_factor=0.0310559 nf=-114 time=161 busy=0 rx=5
202  * ACS:  3: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=161 busy=0 rx=0
203  * ACS:  4: min_nf=-114 interference_factor=0.00617284 nf=-114 time=162 busy=0 rx=1
204  * ACS:  5: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0
205  * ACS:  * interference factor average: 0.00993022
206  * ACS: Survey analysis for channel 10 (2457 MHz)
207  * ACS:  1: min_nf=-114 interference_factor=0.00621118 nf=-114 time=161 busy=0 rx=1
208  * ACS:  2: min_nf=-114 interference_factor=0.00621118 nf=-114 time=161 busy=0 rx=1
209  * ACS:  3: min_nf=-114 interference_factor=0.00621118 nf=-114 time=161 busy=0 rx=1
210  * ACS:  4: min_nf=-114 interference_factor=0.0493827 nf=-114 time=162 busy=0 rx=8
211  * ACS:  5: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0
212  * ACS:  * interference factor average: 0.0136033
213  * ACS: Survey analysis for channel 11 (2462 MHz)
214  * ACS:  1: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=161 busy=0 rx=0
215  * ACS:  2: min_nf=-114 interference_factor=2.51189e-23 nf=-113 time=161 busy=0 rx=0
216  * ACS:  3: min_nf=-114 interference_factor=2.51189e-23 nf=-113 time=161 busy=0 rx=0
217  * ACS:  4: min_nf=-114 interference_factor=0.0432099 nf=-114 time=162 busy=0 rx=7
218  * ACS:  5: min_nf=-114 interference_factor=0.0925926 nf=-114 time=162 busy=0 rx=15
219  * ACS:  * interference factor average: 0.0271605
220  * ACS: Survey analysis for channel 12 (2467 MHz)
221  * ACS:  1: min_nf=-114 interference_factor=0.0621118 nf=-113 time=161 busy=0 rx=10
222  * ACS:  2: min_nf=-114 interference_factor=0.00621118 nf=-114 time=161 busy=0 rx=1
223  * ACS:  3: min_nf=-114 interference_factor=2.51189e-23 nf=-113 time=162 busy=0 rx=0
224  * ACS:  4: min_nf=-114 interference_factor=2.51189e-23 nf=-113 time=162 busy=0 rx=0
225  * ACS:  5: min_nf=-114 interference_factor=0.00617284 nf=-113 time=162 busy=0 rx=1
226  * ACS:  * interference factor average: 0.0148992
227  * ACS: Survey analysis for channel 13 (2472 MHz)
228  * ACS:  1: min_nf=-114 interference_factor=0.0745342 nf=-114 time=161 busy=0 rx=12
229  * ACS:  2: min_nf=-114 interference_factor=0.0555556 nf=-114 time=162 busy=0 rx=9
230  * ACS:  3: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0
231  * ACS:  4: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0
232  * ACS:  5: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0
233  * ACS:  * interference factor average: 0.0260179
234  * ACS: Survey analysis for selected bandwidth 20MHz
235  * ACS:  * channel 1: total interference = 0.121432
236  * ACS:  * channel 2: total interference = 0.137512
237  * ACS:  * channel 3: total interference = 0.369757
238  * ACS:  * channel 4: total interference = 0.546338
239  * ACS:  * channel 5: total interference = 0.690538
240  * ACS:  * channel 6: total interference = 0.762242
241  * ACS:  * channel 7: total interference = 0.756092
242  * ACS:  * channel 8: total interference = 0.537451
243  * ACS:  * channel 9: total interference = 0.332313
244  * ACS:  * channel 10: total interference = 0.152182
245  * ACS:  * channel 11: total interference = 0.0916111
246  * ACS:  * channel 12: total interference = 0.0816809
247  * ACS:  * channel 13: total interference = 0.0680776
248  * ACS: Ideal channel is 13 (2472 MHz) with total interference factor of 0.0680776
249  *
250  * [1] http://en.wikipedia.org/wiki/Near_and_far_field
251  */
252 
253 enum bw_type {
254 	ACS_BW40,
255 	ACS_BW80,
256 	ACS_BW160,
257 	ACS_BW320_1,
258 	ACS_BW320_2,
259 };
260 
261 struct bw_item {
262 	int first;
263 	int last;
264 	int center_chan;
265 };
266 
267 static const struct bw_item bw_40[] = {
268 	{ 5180, 5200, 38 }, { 5220, 5240, 46 }, { 5260, 5280, 54 },
269 	{ 5300, 5320, 62 }, { 5500, 5520, 102 }, { 5540, 5560, 110 },
270 	{ 5580, 5600, 118 }, { 5620, 5640, 126 }, { 5660, 5680, 134 },
271 	{ 5700, 5720, 142 }, { 5745, 5765, 151 }, { 5785, 5805, 159 },
272 	{ 5825, 5845, 167 }, { 5865, 5885, 175 },
273 	{ 5955, 5975, 3 }, { 5995, 6015, 11 }, { 6035, 6055, 19 },
274 	{ 6075, 6095, 27 }, { 6115, 6135, 35 }, { 6155, 6175, 43 },
275 	{ 6195, 6215, 51 }, { 6235, 6255, 59 }, { 6275, 6295, 67 },
276 	{ 6315, 6335, 75 }, { 6355, 6375, 83 }, { 6395, 6415, 91 },
277 	{ 6435, 6455, 99 }, { 6475, 6495, 107 }, { 6515, 6535, 115 },
278 	{ 6555, 6575, 123 }, { 6595, 6615, 131 }, { 6635, 6655, 139 },
279 	{ 6675, 6695, 147 }, { 6715, 6735, 155 }, { 6755, 6775, 163 },
280 	{ 6795, 6815, 171 }, { 6835, 6855, 179 }, { 6875, 6895, 187 },
281 	{ 6915, 6935, 195 }, { 6955, 6975, 203 }, { 6995, 7015, 211 },
282 	{ 7035, 7055, 219 }, { 7075, 7095, 227}, { -1, -1, -1 }
283 };
284 static const struct bw_item bw_80[] = {
285 	{ 5180, 5240, 42 }, { 5260, 5320, 58 }, { 5500, 5560, 106 },
286 	{ 5580, 5640, 122 }, { 5660, 5720, 138 }, { 5745, 5805, 155 },
287 	{ 5825, 5885, 171},
288 	{ 5955, 6015, 7 }, { 6035, 6095, 23 }, { 6115, 6175, 39 },
289 	{ 6195, 6255, 55 }, { 6275, 6335, 71 }, { 6355, 6415, 87 },
290 	{ 6435, 6495, 103 }, { 6515, 6575, 119 }, { 6595, 6655, 135 },
291 	{ 6675, 6735, 151 }, { 6755, 6815, 167 }, { 6835, 6895, 183 },
292 	{ 6915, 6975, 199 }, { 6995, 7055, 215 }, { -1, -1, -1 }
293 };
294 static const struct bw_item bw_160[] = {
295 	{ 5180, 5320, 50 }, { 5500, 5640, 114 }, { 5745, 5885, 163 },
296 	{ 5955, 6095, 15 }, { 6115, 6255, 47 }, { 6275, 6415, 79 },
297 	{ 6435, 6575, 111 }, { 6595, 6735, 143 },
298 	{ 6755, 6895, 175 }, { 6915, 7055, 207 }, { -1, -1, -1 }
299 };
300 static const struct bw_item bw_320_1[] = {
301 	{ 5955, 6255, 31 }, { 6275, 6575, 95 }, { 6595, 6895, 159 },
302 	{ -1, -1, -1 }
303 };
304 static const struct bw_item bw_320_2[] = {
305 	{ 6115, 6415, 63 }, { 6435, 6735, 127 }, { 6755, 7055, 191 },
306 	{ -1, -1, -1 }
307 };
308 static const struct bw_item *bw_desc[] = {
309 	[ACS_BW40] = bw_40,
310 	[ACS_BW80] = bw_80,
311 	[ACS_BW160] = bw_160,
312 	[ACS_BW320_1] = bw_320_1,
313 	[ACS_BW320_2] = bw_320_2,
314 };
315 
316 
317 static int acs_request_scan(struct hostapd_iface *iface);
318 static int acs_survey_is_sufficient(struct freq_survey *survey);
319 static void acs_scan_retry(void *eloop_data, void *user_data);
320 
321 
acs_clean_chan_surveys(struct hostapd_channel_data * chan)322 static void acs_clean_chan_surveys(struct hostapd_channel_data *chan)
323 {
324 	struct freq_survey *survey, *tmp;
325 
326 	if (dl_list_empty(&chan->survey_list))
327 		return;
328 
329 	dl_list_for_each_safe(survey, tmp, &chan->survey_list,
330 			      struct freq_survey, list) {
331 		dl_list_del(&survey->list);
332 		os_free(survey);
333 	}
334 }
335 
336 
acs_cleanup_mode(struct hostapd_hw_modes * mode)337 static void acs_cleanup_mode(struct hostapd_hw_modes *mode)
338 {
339 	int i;
340 	struct hostapd_channel_data *chan;
341 
342 	for (i = 0; i < mode->num_channels; i++) {
343 		chan = &mode->channels[i];
344 
345 		if (chan->flag & HOSTAPD_CHAN_SURVEY_LIST_INITIALIZED)
346 			acs_clean_chan_surveys(chan);
347 
348 		dl_list_init(&chan->survey_list);
349 		chan->flag |= HOSTAPD_CHAN_SURVEY_LIST_INITIALIZED;
350 		chan->min_nf = 0;
351 		chan->punct_bitmap = 0;
352 	}
353 }
354 
355 
acs_cleanup(struct hostapd_iface * iface)356 void acs_cleanup(struct hostapd_iface *iface)
357 {
358 	int i;
359 
360 	for (i = 0; i < iface->num_hw_features; i++)
361 		acs_cleanup_mode(&iface->hw_features[i]);
362 
363 	iface->chans_surveyed = 0;
364 	iface->acs_num_completed_scans = 0;
365 	iface->acs_num_retries = 0;
366 	eloop_cancel_timeout(acs_scan_retry, iface, NULL);
367 }
368 
369 
acs_fail(struct hostapd_iface * iface)370 static void acs_fail(struct hostapd_iface *iface)
371 {
372 	wpa_printf(MSG_ERROR, "ACS: Failed to start");
373 	acs_cleanup(iface);
374 	hostapd_disable_iface(iface);
375 }
376 
377 
378 static long double
acs_survey_interference_factor(struct freq_survey * survey,s8 min_nf)379 acs_survey_interference_factor(struct freq_survey *survey, s8 min_nf)
380 {
381 	long double factor, busy, total;
382 
383 	if (survey->filled & SURVEY_HAS_CHAN_TIME_BUSY)
384 		busy = survey->channel_time_busy;
385 	else if (survey->filled & SURVEY_HAS_CHAN_TIME_RX)
386 		busy = survey->channel_time_rx;
387 	else {
388 		wpa_printf(MSG_ERROR, "ACS: Survey data missing");
389 		return 0;
390 	}
391 
392 	total = survey->channel_time;
393 
394 	if (survey->filled & SURVEY_HAS_CHAN_TIME_TX) {
395 		busy -= survey->channel_time_tx;
396 		total -= survey->channel_time_tx;
397 	}
398 
399 	/* TODO: figure out the best multiplier for noise floor base */
400 	factor = pow(10, survey->nf / 5.0L) +
401 		(total ? (busy / total) : 0) *
402 		pow(2, pow(10, (long double) survey->nf / 10.0L) -
403 		    pow(10, (long double) min_nf / 10.0L));
404 
405 	return factor;
406 }
407 
408 
409 static void
acs_survey_chan_interference_factor(struct hostapd_iface * iface,struct hostapd_channel_data * chan)410 acs_survey_chan_interference_factor(struct hostapd_iface *iface,
411 				    struct hostapd_channel_data *chan)
412 {
413 	struct freq_survey *survey;
414 	unsigned int i = 0;
415 	long double int_factor = 0;
416 	unsigned count = 0;
417 
418 	if (dl_list_empty(&chan->survey_list) ||
419 	    (chan->flag & HOSTAPD_CHAN_DISABLED))
420 		return;
421 
422 	chan->interference_factor = 0;
423 
424 	dl_list_for_each(survey, &chan->survey_list, struct freq_survey, list)
425 	{
426 		i++;
427 
428 		if (!acs_survey_is_sufficient(survey)) {
429 			wpa_printf(MSG_DEBUG, "ACS: %d: insufficient data", i);
430 			continue;
431 		}
432 
433 		count++;
434 		int_factor = acs_survey_interference_factor(survey,
435 							    iface->lowest_nf);
436 		chan->interference_factor += int_factor;
437 		wpa_printf(MSG_DEBUG, "ACS: %d: min_nf=%d interference_factor=%Lg nf=%d time=%lu busy=%lu rx=%lu",
438 			   i, chan->min_nf, int_factor,
439 			   survey->nf, (unsigned long) survey->channel_time,
440 			   (unsigned long) survey->channel_time_busy,
441 			   (unsigned long) survey->channel_time_rx);
442 	}
443 
444 	if (count)
445 		chan->interference_factor /= count;
446 }
447 
448 
acs_usable_bw_chan(const struct hostapd_channel_data * chan,enum bw_type bw)449 static bool acs_usable_bw_chan(const struct hostapd_channel_data *chan,
450 			       enum bw_type bw)
451 {
452 	unsigned int i = 0;
453 
454 	while (bw_desc[bw][i].first != -1) {
455 		if (chan->freq == bw_desc[bw][i].first)
456 			return true;
457 		i++;
458 	}
459 
460 	return false;
461 }
462 
463 
acs_get_bw_center_chan(int freq,enum bw_type bw)464 static int acs_get_bw_center_chan(int freq, enum bw_type bw)
465 {
466 	unsigned int i = 0;
467 
468 	while (bw_desc[bw][i].first != -1) {
469 		if (freq >= bw_desc[bw][i].first &&
470 		    freq <= bw_desc[bw][i].last)
471 			return bw_desc[bw][i].center_chan;
472 		i++;
473 	}
474 
475 	return 0;
476 }
477 
478 
acs_survey_is_sufficient(struct freq_survey * survey)479 static int acs_survey_is_sufficient(struct freq_survey *survey)
480 {
481 	if (!(survey->filled & SURVEY_HAS_NF)) {
482 		wpa_printf(MSG_INFO,
483 			   "ACS: Survey for freq %d is missing noise floor",
484 			   survey->freq);
485 		return 0;
486 	}
487 
488 	if (!(survey->filled & SURVEY_HAS_CHAN_TIME)) {
489 		wpa_printf(MSG_INFO,
490 			   "ACS: Survey for freq %d is missing channel time",
491 			   survey->freq);
492 		return 0;
493 	}
494 
495 	if (!(survey->filled & SURVEY_HAS_CHAN_TIME_BUSY) &&
496 	    !(survey->filled & SURVEY_HAS_CHAN_TIME_RX)) {
497 		wpa_printf(MSG_INFO,
498 			   "ACS: Survey for freq %d is missing RX and busy time (at least one is required)",
499 			   survey->freq);
500 		return 0;
501 	}
502 
503 	return 1;
504 }
505 
506 
acs_survey_list_is_sufficient(struct hostapd_channel_data * chan)507 static int acs_survey_list_is_sufficient(struct hostapd_channel_data *chan)
508 {
509 	struct freq_survey *survey;
510 	int ret = -1;
511 
512 	dl_list_for_each(survey, &chan->survey_list, struct freq_survey, list)
513 	{
514 		if (acs_survey_is_sufficient(survey)) {
515 			ret = 1;
516 			break;
517 		}
518 		ret = 0;
519 	}
520 
521 	if (ret == -1)
522 		ret = 0; /* no survey list entries */
523 
524 	if (!ret) {
525 		wpa_printf(MSG_INFO,
526 			   "ACS: Channel %d has insufficient survey data",
527 			   chan->chan);
528 	}
529 
530 	return ret;
531 }
532 
533 
acs_surveys_are_sufficient_mode(struct hostapd_hw_modes * mode)534 static int acs_surveys_are_sufficient_mode(struct hostapd_hw_modes *mode)
535 {
536 	int i;
537 	struct hostapd_channel_data *chan;
538 
539 	for (i = 0; i < mode->num_channels; i++) {
540 		chan = &mode->channels[i];
541 		if (!(chan->flag & HOSTAPD_CHAN_DISABLED) &&
542 		    acs_survey_list_is_sufficient(chan))
543 			return 1;
544 	}
545 
546 	return 0;
547 }
548 
549 
acs_surveys_are_sufficient(struct hostapd_iface * iface)550 static int acs_surveys_are_sufficient(struct hostapd_iface *iface)
551 {
552 	int i;
553 	struct hostapd_hw_modes *mode;
554 
555 	for (i = 0; i < iface->num_hw_features; i++) {
556 		mode = &iface->hw_features[i];
557 		if (!hostapd_hw_skip_mode(iface, mode) &&
558 		    acs_surveys_are_sufficient_mode(mode))
559 			return 1;
560 	}
561 
562 	return 0;
563 }
564 
565 
acs_usable_chan(struct hostapd_channel_data * chan)566 static int acs_usable_chan(struct hostapd_channel_data *chan)
567 {
568 	return !dl_list_empty(&chan->survey_list) &&
569 		!(chan->flag & HOSTAPD_CHAN_DISABLED) &&
570 		acs_survey_list_is_sufficient(chan);
571 }
572 
573 
is_in_chanlist(struct hostapd_iface * iface,struct hostapd_channel_data * chan)574 static int is_in_chanlist(struct hostapd_iface *iface,
575 			  struct hostapd_channel_data *chan)
576 {
577 	if (!iface->conf->acs_ch_list.num)
578 		return 1;
579 
580 	return freq_range_list_includes(&iface->conf->acs_ch_list, chan->chan);
581 }
582 
583 
is_in_freqlist(struct hostapd_iface * iface,struct hostapd_channel_data * chan)584 static int is_in_freqlist(struct hostapd_iface *iface,
585 			  struct hostapd_channel_data *chan)
586 {
587 	if (!iface->conf->acs_freq_list.num)
588 		return 1;
589 
590 	return freq_range_list_includes(&iface->conf->acs_freq_list,
591 					chan->freq);
592 }
593 
594 
acs_survey_mode_interference_factor(struct hostapd_iface * iface,struct hostapd_hw_modes * mode)595 static void acs_survey_mode_interference_factor(
596 	struct hostapd_iface *iface, struct hostapd_hw_modes *mode)
597 {
598 	int i;
599 	struct hostapd_channel_data *chan;
600 
601 	for (i = 0; i < mode->num_channels; i++) {
602 		chan = &mode->channels[i];
603 
604 		if (!acs_usable_chan(chan))
605 			continue;
606 
607 		if ((chan->flag & HOSTAPD_CHAN_RADAR) &&
608 		    iface->conf->acs_exclude_dfs)
609 			continue;
610 
611 		if (!is_in_chanlist(iface, chan))
612 			continue;
613 
614 		if (!is_in_freqlist(iface, chan))
615 			continue;
616 
617 		if (chan->max_tx_power < iface->conf->min_tx_power)
618 			continue;
619 
620 		if ((chan->flag & HOSTAPD_CHAN_INDOOR_ONLY) &&
621 		    iface->conf->country[2] == 0x4f)
622 			continue;
623 
624 		wpa_printf(MSG_DEBUG, "ACS: Survey analysis for channel %d (%d MHz)",
625 			   chan->chan, chan->freq);
626 
627 		acs_survey_chan_interference_factor(iface, chan);
628 
629 		wpa_printf(MSG_DEBUG, "ACS:  * interference factor average: %Lg",
630 			   chan->interference_factor);
631 	}
632 }
633 
634 
acs_survey_all_chans_interference_factor(struct hostapd_iface * iface)635 static void acs_survey_all_chans_interference_factor(
636 	struct hostapd_iface *iface)
637 {
638 	int i;
639 	struct hostapd_hw_modes *mode;
640 
641 	for (i = 0; i < iface->num_hw_features; i++) {
642 		mode = &iface->hw_features[i];
643 		if (!hostapd_hw_skip_mode(iface, mode))
644 			acs_survey_mode_interference_factor(iface, mode);
645 	}
646 }
647 
648 
649 static struct hostapd_channel_data *
acs_find_chan_mode(struct hostapd_hw_modes * mode,int freq)650 acs_find_chan_mode(struct hostapd_hw_modes *mode, int freq)
651 {
652 	struct hostapd_channel_data *chan;
653 	int i;
654 
655 	for (i = 0; i < mode->num_channels; i++) {
656 		chan = &mode->channels[i];
657 
658 		if (chan->flag & HOSTAPD_CHAN_DISABLED)
659 			continue;
660 
661 		if (chan->freq == freq)
662 			return chan;
663 	}
664 
665 	return NULL;
666 }
667 
668 
669 static enum hostapd_hw_mode
acs_find_mode(struct hostapd_iface * iface,int freq)670 acs_find_mode(struct hostapd_iface *iface, int freq)
671 {
672 	int i;
673 	struct hostapd_hw_modes *mode;
674 	struct hostapd_channel_data *chan;
675 
676 	for (i = 0; i < iface->num_hw_features; i++) {
677 		mode = &iface->hw_features[i];
678 		if (!hostapd_hw_skip_mode(iface, mode)) {
679 			chan = acs_find_chan_mode(mode, freq);
680 			if (chan)
681 				return mode->mode;
682 		}
683 	}
684 
685 	return HOSTAPD_MODE_IEEE80211ANY;
686 }
687 
688 
689 static struct hostapd_channel_data *
acs_find_chan(struct hostapd_iface * iface,int freq)690 acs_find_chan(struct hostapd_iface *iface, int freq)
691 {
692 	int i;
693 	struct hostapd_hw_modes *mode;
694 	struct hostapd_channel_data *chan;
695 
696 	for (i = 0; i < iface->num_hw_features; i++) {
697 		mode = &iface->hw_features[i];
698 		if (!hostapd_hw_skip_mode(iface, mode)) {
699 			chan = acs_find_chan_mode(mode, freq);
700 			if (chan)
701 				return chan;
702 		}
703 	}
704 
705 	return NULL;
706 }
707 
708 
is_24ghz_mode(enum hostapd_hw_mode mode)709 static int is_24ghz_mode(enum hostapd_hw_mode mode)
710 {
711 	return mode == HOSTAPD_MODE_IEEE80211B ||
712 		mode == HOSTAPD_MODE_IEEE80211G;
713 }
714 
715 
is_common_24ghz_chan(int chan)716 static int is_common_24ghz_chan(int chan)
717 {
718 	return chan == 1 || chan == 6 || chan == 11;
719 }
720 
721 
722 #ifndef ACS_ADJ_WEIGHT
723 #define ACS_ADJ_WEIGHT 0.85
724 #endif /* ACS_ADJ_WEIGHT */
725 
726 #ifndef ACS_NEXT_ADJ_WEIGHT
727 #define ACS_NEXT_ADJ_WEIGHT 0.55
728 #endif /* ACS_NEXT_ADJ_WEIGHT */
729 
730 #ifndef ACS_24GHZ_PREFER_1_6_11
731 /*
732  * Select commonly used channels 1, 6, 11 by default even if a neighboring
733  * channel has a smaller interference factor as long as it is not better by more
734  * than this multiplier.
735  */
736 #define ACS_24GHZ_PREFER_1_6_11 0.8
737 #endif /* ACS_24GHZ_PREFER_1_6_11 */
738 
739 
740 #ifdef CONFIG_IEEE80211BE
acs_update_puncturing_bitmap(struct hostapd_iface * iface,struct hostapd_hw_modes * mode,u32 bw,int n_chans,struct hostapd_channel_data * chan,long double factor,int index_primary)741 static void acs_update_puncturing_bitmap(struct hostapd_iface *iface,
742 					 struct hostapd_hw_modes *mode, u32 bw,
743 					 int n_chans,
744 					 struct hostapd_channel_data *chan,
745 					 long double factor,
746 					 int index_primary)
747 {
748 	struct hostapd_config *conf = iface->conf;
749 	struct hostapd_channel_data *adj_chan = NULL, *first_chan = chan;
750 	int i;
751 	long double threshold;
752 
753 	/*
754 	 * If threshold is 0 or user configured puncturing pattern is
755 	 * available then don't add additional puncturing.
756 	 */
757 	if (!conf->punct_acs_threshold || conf->punct_bitmap)
758 		return;
759 
760 	if (is_24ghz_mode(mode->mode) || bw < 80)
761 		return;
762 
763 	threshold = factor * conf->punct_acs_threshold / 100;
764 	for (i = 0; i < n_chans; i++) {
765 		int adj_freq;
766 
767 		if (i == index_primary)
768 			continue; /* Cannot puncture primary channel */
769 
770 		if (i > index_primary)
771 			adj_freq = chan->freq + (i - index_primary) * 20;
772 		else
773 			adj_freq = chan->freq - (index_primary - i) * 20;
774 
775 		adj_chan = acs_find_chan(iface, adj_freq);
776 		if (!adj_chan) {
777 			chan->punct_bitmap = 0;
778 			return;
779 		}
780 
781 		if (i == 0)
782 			first_chan = adj_chan;
783 
784 		if (adj_chan->interference_factor > threshold)
785 			chan->punct_bitmap |= BIT(i);
786 	}
787 
788 	if (!is_punct_bitmap_valid(bw, (chan->freq - first_chan->freq) / 20,
789 				   chan->punct_bitmap))
790 		chan->punct_bitmap = 0;
791 }
792 #endif /* CONFIG_IEEE80211BE */
793 
794 
795 static bool
acs_usable_bw320_chan(struct hostapd_iface * iface,struct hostapd_channel_data * chan,int * bw320_offset)796 acs_usable_bw320_chan(struct hostapd_iface *iface,
797 		      struct hostapd_channel_data *chan, int *bw320_offset)
798 {
799 	const char *bw320_str[] = { "320 MHz", "320 MHz-1", "320 MHz-2" };
800 	int conf_bw320_offset = hostapd_get_bw320_offset(iface->conf);
801 
802 	*bw320_offset = 0;
803 	switch (conf_bw320_offset) {
804 	case 1:
805 		if (acs_usable_bw_chan(chan, ACS_BW320_1))
806 			*bw320_offset = 1;
807 		break;
808 	case 2:
809 		if (acs_usable_bw_chan(chan, ACS_BW320_2))
810 			*bw320_offset = 2;
811 		break;
812 	case 0:
813 	default:
814 		conf_bw320_offset = 0;
815 		if (acs_usable_bw_chan(chan, ACS_BW320_1))
816 			*bw320_offset = 1;
817 		else if (acs_usable_bw_chan(chan, ACS_BW320_2))
818 			*bw320_offset = 2;
819 		break;
820 	}
821 
822 	if (!*bw320_offset)
823 		wpa_printf(MSG_DEBUG,
824 			   "ACS: Channel %d: not allowed as primary channel for %s bandwidth",
825 			   chan->chan, bw320_str[conf_bw320_offset]);
826 
827 	return *bw320_offset != 0;
828 }
829 
830 
831 static void
acs_find_ideal_chan_mode(struct hostapd_iface * iface,struct hostapd_hw_modes * mode,int n_chans,u32 bw,struct hostapd_channel_data ** rand_chan,struct hostapd_channel_data ** ideal_chan,long double * ideal_factor)832 acs_find_ideal_chan_mode(struct hostapd_iface *iface,
833 			 struct hostapd_hw_modes *mode,
834 			 int n_chans, u32 bw,
835 			 struct hostapd_channel_data **rand_chan,
836 			 struct hostapd_channel_data **ideal_chan,
837 			 long double *ideal_factor)
838 {
839 	struct hostapd_channel_data *chan, *adj_chan = NULL, *best;
840 	long double factor;
841 	int i, j;
842 	int bw320_offset = 0, ideal_bw320_offset = 0;
843 	unsigned int k;
844 	int secondary_channel = 1, freq_offset;
845 #ifdef CONFIG_IEEE80211BE
846 	int index_primary = 0;
847 #endif /* CONFIG_IEEE80211BE */
848 
849 	if (is_24ghz_mode(mode->mode))
850 		secondary_channel = iface->conf->secondary_channel;
851 
852 	for (i = 0; i < mode->num_channels; i++) {
853 		double total_weight = 0;
854 		struct acs_bias *bias, tmp_bias;
855 
856 		chan = &mode->channels[i];
857 
858 		/* Since in the current ACS implementation the first channel is
859 		 * always a primary channel, skip channels not available as
860 		 * primary until more sophisticated channel selection is
861 		 * implemented.
862 		 *
863 		 * If this implementation is changed to allow any channel in
864 		 * the bandwidth to be the primary one, the last parameter to
865 		 * acs_update_puncturing_bitmap() should be changed to the index
866 		 * of the primary channel
867 		 */
868 		if (!chan_pri_allowed(chan))
869 			continue;
870 
871 		if ((chan->flag & HOSTAPD_CHAN_RADAR) &&
872 		    iface->conf->acs_exclude_dfs)
873 			continue;
874 
875 		if (!is_in_chanlist(iface, chan))
876 			continue;
877 
878 		if (!is_in_freqlist(iface, chan))
879 			continue;
880 
881 		if (chan->max_tx_power < iface->conf->min_tx_power)
882 			continue;
883 
884 		if ((chan->flag & HOSTAPD_CHAN_INDOOR_ONLY) &&
885 		    iface->conf->country[2] == 0x4f)
886 			continue;
887 
888 		if (!chan_bw_allowed(chan, bw, secondary_channel != -1, 1)) {
889 			wpa_printf(MSG_DEBUG,
890 				   "ACS: Channel %d: BW %u is not supported",
891 				   chan->chan, bw);
892 			continue;
893 		}
894 
895 		/* HT40 on 5 GHz has a limited set of primary channels as per
896 		 * 11n Annex J */
897 		if (mode->mode == HOSTAPD_MODE_IEEE80211A &&
898 		    ((iface->conf->ieee80211n &&
899 		      iface->conf->secondary_channel) ||
900 		     is_6ghz_freq(chan->freq)) &&
901 		    !acs_usable_bw_chan(chan, ACS_BW40)) {
902 			wpa_printf(MSG_DEBUG,
903 				   "ACS: Channel %d: not allowed as primary channel for 40 MHz bandwidth",
904 				   chan->chan);
905 			continue;
906 		}
907 
908 		if (mode->mode == HOSTAPD_MODE_IEEE80211A &&
909 		    (iface->conf->ieee80211ac || iface->conf->ieee80211ax ||
910 		     iface->conf->ieee80211be)) {
911 			if (hostapd_get_oper_chwidth(iface->conf) ==
912 			    CONF_OPER_CHWIDTH_80MHZ &&
913 			    !acs_usable_bw_chan(chan, ACS_BW80)) {
914 				wpa_printf(MSG_DEBUG,
915 					   "ACS: Channel %d: not allowed as primary channel for 80 MHz bandwidth",
916 					   chan->chan);
917 				continue;
918 			}
919 
920 			if (hostapd_get_oper_chwidth(iface->conf) ==
921 			    CONF_OPER_CHWIDTH_160MHZ &&
922 			    !acs_usable_bw_chan(chan, ACS_BW160)) {
923 				wpa_printf(MSG_DEBUG,
924 					   "ACS: Channel %d: not allowed as primary channel for 160 MHz bandwidth",
925 					   chan->chan);
926 				continue;
927 			}
928 		}
929 
930 		if (mode->mode == HOSTAPD_MODE_IEEE80211A &&
931 		    iface->conf->ieee80211be) {
932 			if (hostapd_get_oper_chwidth(iface->conf) ==
933 			    CONF_OPER_CHWIDTH_320MHZ &&
934 			    !acs_usable_bw320_chan(iface, chan, &bw320_offset))
935 				continue;
936 		}
937 
938 		factor = 0;
939 		best = NULL;
940 		if (acs_usable_chan(chan)) {
941 			factor = chan->interference_factor;
942 			total_weight = 1;
943 			best = chan;
944 		}
945 
946 		for (j = 1; j < n_chans; j++) {
947 			adj_chan = acs_find_chan(iface, chan->freq +
948 						 j * secondary_channel * 20);
949 			if (!adj_chan)
950 				break;
951 
952 			if (!chan_bw_allowed(adj_chan, bw, 1, 0)) {
953 				wpa_printf(MSG_DEBUG,
954 					   "ACS: PRI Channel %d: secondary channel %d BW %u is not supported",
955 					   chan->chan, adj_chan->chan, bw);
956 				break;
957 			}
958 
959 			if (!acs_usable_chan(adj_chan))
960 				continue;
961 
962 			factor += adj_chan->interference_factor;
963 			total_weight += 1;
964 
965 			/* find the best channel in this segment */
966 			if (!best || adj_chan->interference_factor <
967 			    best->interference_factor)
968 				best = adj_chan;
969 		}
970 
971 		if (j != n_chans) {
972 			wpa_printf(MSG_DEBUG, "ACS: Channel %d: not enough bandwidth",
973 				   chan->chan);
974 			continue;
975 		}
976 
977 		/* If the AP is in the 5 GHz or 6 GHz band, lets prefer a less
978 		 * crowded primary channel if one was found in the segment */
979 		if (iface->current_mode &&
980 		    iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A &&
981 		    best && chan != best) {
982 			wpa_printf(MSG_DEBUG,
983 				   "ACS: promoting channel %d over %d (less interference %Lg/%Lg)",
984 				   best->chan, chan->chan,
985 				   chan->interference_factor,
986 				   best->interference_factor);
987 #ifdef CONFIG_IEEE80211BE
988 			index_primary = (chan->freq - best->freq) / 20;
989 #endif /* CONFIG_IEEE80211BE */
990 			chan = best;
991 		}
992 
993 		/* 2.4 GHz has overlapping 20 MHz channels. Include adjacent
994 		 * channel interference factor. */
995 		if (is_24ghz_mode(mode->mode)) {
996 			for (j = 0; j < n_chans; j++) {
997 				freq_offset = j * 20 * secondary_channel;
998 				adj_chan = acs_find_chan(iface, chan->freq +
999 							 freq_offset - 5);
1000 				if (adj_chan && acs_usable_chan(adj_chan)) {
1001 					factor += ACS_ADJ_WEIGHT *
1002 						adj_chan->interference_factor;
1003 					total_weight += ACS_ADJ_WEIGHT;
1004 				}
1005 
1006 				adj_chan = acs_find_chan(iface, chan->freq +
1007 							 freq_offset - 10);
1008 				if (adj_chan && acs_usable_chan(adj_chan)) {
1009 					factor += ACS_NEXT_ADJ_WEIGHT *
1010 						adj_chan->interference_factor;
1011 					total_weight += ACS_NEXT_ADJ_WEIGHT;
1012 				}
1013 
1014 				adj_chan = acs_find_chan(iface, chan->freq +
1015 							 freq_offset + 5);
1016 				if (adj_chan && acs_usable_chan(adj_chan)) {
1017 					factor += ACS_ADJ_WEIGHT *
1018 						adj_chan->interference_factor;
1019 					total_weight += ACS_ADJ_WEIGHT;
1020 				}
1021 
1022 				adj_chan = acs_find_chan(iface, chan->freq +
1023 							 freq_offset + 10);
1024 				if (adj_chan && acs_usable_chan(adj_chan)) {
1025 					factor += ACS_NEXT_ADJ_WEIGHT *
1026 						adj_chan->interference_factor;
1027 					total_weight += ACS_NEXT_ADJ_WEIGHT;
1028 				}
1029 			}
1030 		}
1031 
1032 		if (total_weight == 0)
1033 			continue;
1034 
1035 		factor /= total_weight;
1036 
1037 		bias = NULL;
1038 		if (iface->conf->acs_chan_bias) {
1039 			for (k = 0; k < iface->conf->num_acs_chan_bias; k++) {
1040 				bias = &iface->conf->acs_chan_bias[k];
1041 				if (bias->channel == chan->chan)
1042 					break;
1043 				bias = NULL;
1044 			}
1045 		} else if (is_24ghz_mode(mode->mode) &&
1046 			   is_common_24ghz_chan(chan->chan)) {
1047 			tmp_bias.channel = chan->chan;
1048 			tmp_bias.bias = ACS_24GHZ_PREFER_1_6_11;
1049 			bias = &tmp_bias;
1050 		}
1051 
1052 		if (bias) {
1053 			factor *= bias->bias;
1054 			wpa_printf(MSG_DEBUG,
1055 				   "ACS:  * channel %d: total interference = %Lg (%f bias)",
1056 				   chan->chan, factor, bias->bias);
1057 		} else {
1058 			wpa_printf(MSG_DEBUG,
1059 				   "ACS:  * channel %d: total interference = %Lg",
1060 				   chan->chan, factor);
1061 		}
1062 
1063 		if (acs_usable_chan(chan) &&
1064 		    (!*ideal_chan || factor < *ideal_factor)) {
1065 			/* Reset puncturing bitmap for the previous ideal
1066 			 * channel */
1067 			if (*ideal_chan)
1068 				(*ideal_chan)->punct_bitmap = 0;
1069 
1070 			*ideal_factor = factor;
1071 			*ideal_chan = chan;
1072 			ideal_bw320_offset = bw320_offset;
1073 
1074 #ifdef CONFIG_IEEE80211BE
1075 			if (iface->conf->ieee80211be)
1076 				acs_update_puncturing_bitmap(iface, mode, bw,
1077 							     n_chans, chan,
1078 							     factor,
1079 							     index_primary);
1080 #endif /* CONFIG_IEEE80211BE */
1081 		}
1082 
1083 		/* This channel would at least be usable */
1084 		if (!(*rand_chan)) {
1085 			*rand_chan = chan;
1086 			ideal_bw320_offset = bw320_offset;
1087 		}
1088 	}
1089 
1090 	hostapd_set_and_check_bw320_offset(iface->conf, ideal_bw320_offset);
1091 }
1092 
1093 
1094 /*
1095  * At this point it's assumed chan->interference_factor has been computed.
1096  * This function should be reusable regardless of interference computation
1097  * option (survey, BSS, spectral, ...). chan->interference factor must be
1098  * summable (i.e., must be always greater than zero).
1099  */
1100 static struct hostapd_channel_data *
acs_find_ideal_chan(struct hostapd_iface * iface)1101 acs_find_ideal_chan(struct hostapd_iface *iface)
1102 {
1103 	struct hostapd_channel_data *ideal_chan = NULL,
1104 		*rand_chan = NULL;
1105 	long double ideal_factor = 0;
1106 	int i;
1107 	int n_chans = 1;
1108 	u32 bw;
1109 	struct hostapd_hw_modes *mode;
1110 
1111 	if (is_6ghz_op_class(iface->conf->op_class)) {
1112 		bw = op_class_to_bandwidth(iface->conf->op_class);
1113 		n_chans = bw / 20;
1114 		goto bw_selected;
1115 	}
1116 
1117 	if (iface->conf->ieee80211n &&
1118 	    iface->conf->secondary_channel)
1119 		n_chans = 2;
1120 
1121 	if (iface->conf->ieee80211ac || iface->conf->ieee80211ax ||
1122 	    iface->conf->ieee80211be) {
1123 		switch (hostapd_get_oper_chwidth(iface->conf)) {
1124 		case CONF_OPER_CHWIDTH_80MHZ:
1125 			n_chans = 4;
1126 			break;
1127 		case CONF_OPER_CHWIDTH_160MHZ:
1128 			n_chans = 8;
1129 			break;
1130 		case CONF_OPER_CHWIDTH_320MHZ:
1131 			n_chans = 16;
1132 			break;
1133 		default:
1134 			break;
1135 		}
1136 	}
1137 
1138 	bw = num_chan_to_bw(n_chans);
1139 
1140 bw_selected:
1141 	/* TODO: VHT/HE80+80. Update acs_adjust_center_freq() too. */
1142 
1143 	wpa_printf(MSG_DEBUG,
1144 		   "ACS: Survey analysis for selected bandwidth %d MHz", bw);
1145 
1146 	for (i = 0; i < iface->num_hw_features; i++) {
1147 		mode = &iface->hw_features[i];
1148 		if (!hostapd_hw_skip_mode(iface, mode))
1149 			acs_find_ideal_chan_mode(iface, mode, n_chans, bw,
1150 						 &rand_chan, &ideal_chan,
1151 						 &ideal_factor);
1152 	}
1153 
1154 	if (ideal_chan) {
1155 		wpa_printf(MSG_DEBUG, "ACS: Ideal channel is %d (%d MHz) with total interference factor of %Lg",
1156 			   ideal_chan->chan, ideal_chan->freq, ideal_factor);
1157 
1158 #ifdef CONFIG_IEEE80211BE
1159 		if (iface->conf->punct_acs_threshold)
1160 			wpa_printf(MSG_DEBUG, "ACS: RU puncturing bitmap 0x%x",
1161 				   ideal_chan->punct_bitmap);
1162 #endif /* CONFIG_IEEE80211BE */
1163 
1164 		return ideal_chan;
1165 	}
1166 
1167 	return rand_chan;
1168 }
1169 
1170 
acs_adjust_secondary(struct hostapd_iface * iface)1171 static void acs_adjust_secondary(struct hostapd_iface *iface)
1172 {
1173 	unsigned int i;
1174 
1175 	/* When working with bandwidth over 20 MHz on the 5 GHz or 6 GHz band,
1176 	 * ACS can return a secondary channel which is not the first channel of
1177 	 * the segment and we need to adjust. */
1178 	if (!iface->conf->secondary_channel ||
1179 	    acs_find_mode(iface, iface->freq) != HOSTAPD_MODE_IEEE80211A)
1180 		return;
1181 
1182 	wpa_printf(MSG_DEBUG,
1183 		   "ACS: Adjusting HT/VHT/HE/EHT secondary frequency");
1184 
1185 	for (i = 0; bw_desc[ACS_BW40][i].first != -1; i++) {
1186 		if (iface->freq == bw_desc[ACS_BW40][i].first)
1187 			iface->conf->secondary_channel = 1;
1188 		else if (iface->freq == bw_desc[ACS_BW40][i].last)
1189 			iface->conf->secondary_channel = -1;
1190 	}
1191 }
1192 
1193 
acs_adjust_center_freq(struct hostapd_iface * iface)1194 static void acs_adjust_center_freq(struct hostapd_iface *iface)
1195 {
1196 	int center;
1197 
1198 	wpa_printf(MSG_DEBUG, "ACS: Adjusting center frequency");
1199 
1200 	switch (hostapd_get_oper_chwidth(iface->conf)) {
1201 	case CONF_OPER_CHWIDTH_USE_HT:
1202 		if (iface->conf->secondary_channel &&
1203 		    iface->freq >= 2400 && iface->freq < 2500)
1204 			center = iface->conf->channel +
1205 				2 * iface->conf->secondary_channel;
1206 		else if (iface->conf->secondary_channel)
1207 			center = acs_get_bw_center_chan(iface->freq, ACS_BW40);
1208 		else
1209 			center = iface->conf->channel;
1210 		break;
1211 	case CONF_OPER_CHWIDTH_80MHZ:
1212 		center = acs_get_bw_center_chan(iface->freq, ACS_BW80);
1213 		break;
1214 	case CONF_OPER_CHWIDTH_160MHZ:
1215 		center = acs_get_bw_center_chan(iface->freq, ACS_BW160);
1216 		break;
1217 	case CONF_OPER_CHWIDTH_320MHZ:
1218 		switch (hostapd_get_bw320_offset(iface->conf)) {
1219 		case 1:
1220 			center = acs_get_bw_center_chan(iface->freq,
1221 							ACS_BW320_1);
1222 			break;
1223 		case 2:
1224 			center = acs_get_bw_center_chan(iface->freq,
1225 							ACS_BW320_2);
1226 			break;
1227 		default:
1228 			wpa_printf(MSG_INFO,
1229 				   "ACS: BW320 offset is not selected");
1230 			return;
1231 		}
1232 
1233 		break;
1234 	default:
1235 		/* TODO: How can this be calculated? Adjust
1236 		 * acs_find_ideal_chan() */
1237 		wpa_printf(MSG_INFO,
1238 			   "ACS: Only VHT20/40/80/160/320 is supported now");
1239 		return;
1240 	}
1241 
1242 	hostapd_set_oper_centr_freq_seg0_idx(iface->conf, center);
1243 }
1244 
1245 
acs_study_survey_based(struct hostapd_iface * iface)1246 static int acs_study_survey_based(struct hostapd_iface *iface)
1247 {
1248 	wpa_printf(MSG_DEBUG, "ACS: Trying survey-based ACS");
1249 
1250 	if (!iface->chans_surveyed) {
1251 		wpa_printf(MSG_ERROR, "ACS: Unable to collect survey data");
1252 		return -1;
1253 	}
1254 
1255 	if (!acs_surveys_are_sufficient(iface)) {
1256 		wpa_printf(MSG_ERROR, "ACS: Surveys have insufficient data");
1257 		return -1;
1258 	}
1259 
1260 	acs_survey_all_chans_interference_factor(iface);
1261 	return 0;
1262 }
1263 
1264 
acs_study_options(struct hostapd_iface * iface)1265 static int acs_study_options(struct hostapd_iface *iface)
1266 {
1267 	if (acs_study_survey_based(iface) == 0)
1268 		return 0;
1269 
1270 	/* TODO: If no surveys are available/sufficient this is a good
1271 	 * place to fallback to BSS-based ACS */
1272 
1273 	return -1;
1274 }
1275 
1276 
acs_study(struct hostapd_iface * iface)1277 static void acs_study(struct hostapd_iface *iface)
1278 {
1279 	struct hostapd_channel_data *ideal_chan;
1280 	int err;
1281 
1282 	err = acs_study_options(iface);
1283 	if (err < 0) {
1284 		wpa_printf(MSG_ERROR, "ACS: All study options have failed");
1285 		goto fail;
1286 	}
1287 
1288 	ideal_chan = acs_find_ideal_chan(iface);
1289 	if (!ideal_chan) {
1290 		wpa_printf(MSG_ERROR, "ACS: Failed to compute ideal channel");
1291 		err = -1;
1292 		goto fail;
1293 	}
1294 
1295 	iface->conf->channel = ideal_chan->chan;
1296 	iface->freq = ideal_chan->freq;
1297 #ifdef CONFIG_IEEE80211BE
1298 	iface->conf->punct_bitmap = ideal_chan->punct_bitmap;
1299 #endif /* CONFIG_IEEE80211BE */
1300 
1301 	if (iface->conf->ieee80211ac || iface->conf->ieee80211ax ||
1302 	    iface->conf->ieee80211be) {
1303 		acs_adjust_secondary(iface);
1304 		acs_adjust_center_freq(iface);
1305 	}
1306 
1307 	err = hostapd_select_hw_mode(iface);
1308 	if (err) {
1309 		wpa_printf(MSG_ERROR,
1310 			   "ACS: Could not (err: %d) select hw_mode for freq=%d channel=%d",
1311 			err, iface->freq, iface->conf->channel);
1312 		err = -1;
1313 		goto fail;
1314 	}
1315 
1316 	err = 0;
1317 fail:
1318 	/*
1319 	 * hostapd_setup_interface_complete() will return -1 on failure,
1320 	 * 0 on success and 0 is HOSTAPD_CHAN_VALID :)
1321 	 */
1322 	if (hostapd_acs_completed(iface, err) == HOSTAPD_CHAN_VALID) {
1323 		acs_cleanup(iface);
1324 		return;
1325 	}
1326 
1327 	/* This can possibly happen if channel parameters (secondary
1328 	 * channel, center frequencies) are misconfigured */
1329 	wpa_printf(MSG_ERROR, "ACS: Possibly channel configuration is invalid, please report this along with your config file.");
1330 	acs_fail(iface);
1331 }
1332 
1333 
acs_scan_complete(struct hostapd_iface * iface)1334 static void acs_scan_complete(struct hostapd_iface *iface)
1335 {
1336 	int err;
1337 
1338 	iface->scan_cb = NULL;
1339 	iface->acs_num_retries = 0;
1340 
1341 	wpa_printf(MSG_DEBUG, "ACS: Using survey based algorithm (acs_num_scans=%d)",
1342 		   iface->conf->acs_num_scans);
1343 
1344 	err = hostapd_drv_get_survey(iface->bss[0], 0);
1345 	if (err) {
1346 		wpa_printf(MSG_ERROR, "ACS: Failed to get survey data");
1347 		goto fail;
1348 	}
1349 
1350 	if (++iface->acs_num_completed_scans < iface->conf->acs_num_scans) {
1351 		err = acs_request_scan(iface);
1352 		if (err && err != -EBUSY) {
1353 			wpa_printf(MSG_ERROR, "ACS: Failed to request scan");
1354 			goto fail;
1355 		}
1356 
1357 		return;
1358 	}
1359 
1360 	acs_study(iface);
1361 	return;
1362 fail:
1363 	hostapd_acs_completed(iface, 1);
1364 	acs_fail(iface);
1365 }
1366 
1367 
acs_request_scan_add_freqs(struct hostapd_iface * iface,struct hostapd_hw_modes * mode,int * freq)1368 static int * acs_request_scan_add_freqs(struct hostapd_iface *iface,
1369 					struct hostapd_hw_modes *mode,
1370 					int *freq)
1371 {
1372 	struct hostapd_channel_data *chan;
1373 	int i;
1374 
1375 	for (i = 0; i < mode->num_channels; i++) {
1376 		chan = &mode->channels[i];
1377 		if ((chan->flag & HOSTAPD_CHAN_DISABLED) ||
1378 		    ((chan->flag & HOSTAPD_CHAN_RADAR) &&
1379 		     iface->conf->acs_exclude_dfs))
1380 			continue;
1381 
1382 		if (!is_in_chanlist(iface, chan))
1383 			continue;
1384 
1385 		if (!is_in_freqlist(iface, chan))
1386 			continue;
1387 
1388 		if (chan->max_tx_power < iface->conf->min_tx_power)
1389 			continue;
1390 
1391 		if ((chan->flag & HOSTAPD_CHAN_INDOOR_ONLY) &&
1392 		    iface->conf->country[2] == 0x4f)
1393 			continue;
1394 
1395 		*freq++ = chan->freq;
1396 	}
1397 
1398 	return freq;
1399 }
1400 
1401 
acs_request_scan(struct hostapd_iface * iface)1402 static int acs_request_scan(struct hostapd_iface *iface)
1403 {
1404 	struct wpa_driver_scan_params params;
1405 	int i, *freq, ret;
1406 	int num_channels;
1407 	struct hostapd_hw_modes *mode;
1408 
1409 	os_memset(&params, 0, sizeof(params));
1410 
1411 	num_channels = 0;
1412 	for (i = 0; i < iface->num_hw_features; i++) {
1413 		mode = &iface->hw_features[i];
1414 		if (!hostapd_hw_skip_mode(iface, mode))
1415 			num_channels += mode->num_channels;
1416 	}
1417 
1418 	params.freqs = os_calloc(num_channels + 1, sizeof(params.freqs[0]));
1419 	if (params.freqs == NULL)
1420 		return -1;
1421 
1422 	freq = params.freqs;
1423 
1424 	for (i = 0; i < iface->num_hw_features; i++) {
1425 		mode = &iface->hw_features[i];
1426 		if (!hostapd_hw_skip_mode(iface, mode))
1427 			freq = acs_request_scan_add_freqs(iface, mode, freq);
1428 	}
1429 
1430 	*freq = 0;
1431 
1432 	if (params.freqs == freq) {
1433 		wpa_printf(MSG_ERROR, "ACS: No available channels found");
1434 		os_free(params.freqs);
1435 		return -1;
1436 	}
1437 
1438 	if (!iface->acs_num_retries)
1439 		wpa_printf(MSG_DEBUG, "ACS: Scanning %d / %d",
1440 			   iface->acs_num_completed_scans + 1,
1441 			   iface->conf->acs_num_scans);
1442 	else
1443 		wpa_printf(MSG_DEBUG,
1444 			   "ACS: Re-try scanning attempt %d (%d / %d)",
1445 			   iface->acs_num_retries,
1446 			   iface->acs_num_completed_scans + 1,
1447 			   iface->conf->acs_num_scans);
1448 
1449 	ret = hostapd_driver_scan(iface->bss[0], &params);
1450 	os_free(params.freqs);
1451 
1452 	if (ret == -EBUSY) {
1453 		iface->acs_num_retries++;
1454 		if (iface->acs_num_retries >= ACS_SCAN_RETRY_MAX_COUNT) {
1455 			wpa_printf(MSG_ERROR,
1456 				   "ACS: Failed to request initial scan (all re-attempts failed)");
1457 			acs_fail(iface);
1458 			return -1;
1459 		}
1460 
1461 		wpa_printf(MSG_INFO,
1462 			   "Failed to request acs scan ret=%d (%s) - try to scan after %d seconds",
1463 			   ret, strerror(-ret), ACS_SCAN_RETRY_INTERVAL);
1464 		eloop_cancel_timeout(acs_scan_retry, iface, NULL);
1465 		eloop_register_timeout(ACS_SCAN_RETRY_INTERVAL, 0,
1466 				       acs_scan_retry, iface, NULL);
1467 		return 0;
1468 	}
1469 
1470 	if (ret < 0) {
1471 		wpa_printf(MSG_ERROR, "ACS: Failed to request initial scan");
1472 		acs_cleanup(iface);
1473 		return -1;
1474 	}
1475 
1476 	iface->scan_cb = acs_scan_complete;
1477 
1478 	return 0;
1479 }
1480 
1481 
acs_scan_retry(void * eloop_data,void * user_data)1482 static void acs_scan_retry(void *eloop_data, void *user_data)
1483 {
1484 	struct hostapd_iface *iface = eloop_data;
1485 
1486 	if (acs_request_scan(iface)) {
1487 		wpa_printf(MSG_ERROR,
1488 			   "ACS: Failed to request re-try of initial scan");
1489 		acs_fail(iface);
1490 	}
1491 }
1492 
1493 
acs_init(struct hostapd_iface * iface)1494 enum hostapd_chan_status acs_init(struct hostapd_iface *iface)
1495 {
1496 	int err;
1497 
1498 	wpa_printf(MSG_INFO, "ACS: Automatic channel selection started, this may take a bit");
1499 
1500 	if (iface->drv_flags & WPA_DRIVER_FLAGS_ACS_OFFLOAD) {
1501 		wpa_printf(MSG_INFO, "ACS: Offloading to driver");
1502 
1503 		err = hostapd_drv_do_acs(iface->bss[0]);
1504 		if (err) {
1505 			if (err == 1)
1506 				return HOSTAPD_CHAN_INVALID_NO_IR;
1507 			return HOSTAPD_CHAN_INVALID;
1508 		}
1509 
1510 		return HOSTAPD_CHAN_ACS;
1511 	}
1512 
1513 	if (!iface->current_mode &&
1514 	    iface->conf->hw_mode != HOSTAPD_MODE_IEEE80211ANY)
1515 		return HOSTAPD_CHAN_INVALID;
1516 
1517 	acs_cleanup(iface);
1518 
1519 	if (acs_request_scan(iface) < 0)
1520 		return HOSTAPD_CHAN_INVALID;
1521 
1522 	hostapd_set_state(iface, HAPD_IFACE_ACS);
1523 	wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, ACS_EVENT_STARTED);
1524 
1525 	return HOSTAPD_CHAN_ACS;
1526 }
1527