blob: 8cb5813e99f406caebc1128a2da967fa9e0398a6 [file] [log] [blame]
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001/*
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 "common/ieee802_11_defs.h"
Hai Shalom74f70d42019-02-11 14:42:39 -080016#include "common/hw_features_common.h"
Dmitry Shmidtcce06662013-11-04 18:44:24 -080017#include "common/wpa_ctrl.h"
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070018#include "drivers/driver.h"
19#include "hostapd.h"
20#include "ap_drv_ops.h"
21#include "ap_config.h"
22#include "hw_features.h"
23#include "acs.h"
24
25/*
26 * Automatic Channel Selection
27 * ===========================
28 *
29 * More info at
30 * ------------
31 * http://wireless.kernel.org/en/users/Documentation/acs
32 *
33 * How to use
34 * ----------
35 * - make sure you have CONFIG_ACS=y in hostapd's .config
36 * - use channel=0 or channel=acs to enable ACS
37 *
38 * How does it work
39 * ----------------
40 * 1. passive scans are used to collect survey data
41 * (it is assumed that scan trigger collection of survey data in driver)
42 * 2. interference factor is calculated for each channel
43 * 3. ideal channel is picked depending on channel width by using adjacent
44 * channel interference factors
45 *
46 * Known limitations
47 * -----------------
48 * - Current implementation depends heavily on the amount of time willing to
49 * spend gathering survey data during hostapd startup. Short traffic bursts
50 * may be missed and a suboptimal channel may be picked.
51 * - Ideal channel may end up overlapping a channel with 40 MHz intolerant BSS
52 *
53 * Todo / Ideas
54 * ------------
55 * - implement other interference computation methods
56 * - BSS/RSSI based
57 * - spectral scan based
58 * (should be possibly to hook this up with current ACS scans)
59 * - add wpa_supplicant support (for P2P)
60 * - collect a histogram of interference over time allowing more educated
61 * guess about an ideal channel (perhaps CSA could be used to migrate AP to a
62 * new "better" channel while running)
63 * - include neighboring BSS scan to avoid conflicts with 40 MHz intolerant BSSs
64 * when choosing the ideal channel
65 *
66 * Survey interference factor implementation details
67 * -------------------------------------------------
68 * Generic interference_factor in struct hostapd_channel_data is used.
69 *
70 * The survey interference factor is defined as the ratio of the
71 * observed busy time over the time we spent on the channel,
72 * this value is then amplified by the observed noise floor on
73 * the channel in comparison to the lowest noise floor observed
74 * on the entire band.
75 *
76 * This corresponds to:
77 * ---
78 * (busy time - tx time) / (active time - tx time) * 2^(chan_nf + band_min_nf)
79 * ---
80 *
81 * The coefficient of 2 reflects the way power in "far-field"
82 * radiation decreases as the square of distance from the antenna [1].
83 * What this does is it decreases the observed busy time ratio if the
84 * noise observed was low but increases it if the noise was high,
85 * proportionally to the way "far field" radiation changes over
86 * distance.
87 *
88 * If channel busy time is not available the fallback is to use channel RX time.
89 *
90 * Since noise floor is in dBm it is necessary to convert it into Watts so that
91 * combined channel interference (e.g., HT40, which uses two channels) can be
92 * calculated easily.
93 * ---
94 * (busy time - tx time) / (active time - tx time) *
95 * 2^(10^(chan_nf/10) + 10^(band_min_nf/10))
96 * ---
97 *
98 * However to account for cases where busy/rx time is 0 (channel load is then
99 * 0%) channel noise floor signal power is combined into the equation so a
100 * channel with lower noise floor is preferred. The equation becomes:
101 * ---
102 * 10^(chan_nf/5) + (busy time - tx time) / (active time - tx time) *
103 * 2^(10^(chan_nf/10) + 10^(band_min_nf/10))
104 * ---
105 *
106 * All this "interference factor" is purely subjective and only time
107 * will tell how usable this is. By using the minimum noise floor we
108 * remove any possible issues due to card calibration. The computation
109 * of the interference factor then is dependent on what the card itself
110 * picks up as the minimum noise, not an actual real possible card
111 * noise value.
112 *
113 * Total interference computation details
114 * --------------------------------------
115 * The above channel interference factor is calculated with no respect to
116 * target operational bandwidth.
117 *
118 * To find an ideal channel the above data is combined by taking into account
119 * the target operational bandwidth and selected band. E.g., on 2.4 GHz channels
120 * overlap with 20 MHz bandwidth, but there is no overlap for 20 MHz bandwidth
121 * on 5 GHz.
122 *
123 * Each valid and possible channel spec (i.e., channel + width) is taken and its
124 * interference factor is computed by summing up interferences of each channel
125 * it overlaps. The one with least total interference is picked up.
126 *
127 * Note: This implies base channel interference factor must be non-negative
128 * allowing easy summing up.
129 *
130 * Example ACS analysis printout
131 * -----------------------------
132 *
133 * ACS: Trying survey-based ACS
134 * ACS: Survey analysis for channel 1 (2412 MHz)
135 * ACS: 1: min_nf=-113 interference_factor=0.0802469 nf=-113 time=162 busy=0 rx=13
136 * ACS: 2: min_nf=-113 interference_factor=0.0745342 nf=-113 time=161 busy=0 rx=12
137 * ACS: 3: min_nf=-113 interference_factor=0.0679012 nf=-113 time=162 busy=0 rx=11
138 * ACS: 4: min_nf=-113 interference_factor=0.0310559 nf=-113 time=161 busy=0 rx=5
139 * ACS: 5: min_nf=-113 interference_factor=0.0248447 nf=-113 time=161 busy=0 rx=4
140 * ACS: * interference factor average: 0.0557166
141 * ACS: Survey analysis for channel 2 (2417 MHz)
142 * ACS: 1: min_nf=-113 interference_factor=0.0185185 nf=-113 time=162 busy=0 rx=3
143 * ACS: 2: min_nf=-113 interference_factor=0.0246914 nf=-113 time=162 busy=0 rx=4
144 * ACS: 3: min_nf=-113 interference_factor=0.037037 nf=-113 time=162 busy=0 rx=6
145 * ACS: 4: min_nf=-113 interference_factor=0.149068 nf=-113 time=161 busy=0 rx=24
146 * ACS: 5: min_nf=-113 interference_factor=0.0248447 nf=-113 time=161 busy=0 rx=4
147 * ACS: * interference factor average: 0.050832
148 * ACS: Survey analysis for channel 3 (2422 MHz)
149 * ACS: 1: min_nf=-113 interference_factor=2.51189e-23 nf=-113 time=162 busy=0 rx=0
150 * ACS: 2: min_nf=-113 interference_factor=0.0185185 nf=-113 time=162 busy=0 rx=3
151 * ACS: 3: min_nf=-113 interference_factor=0.0186335 nf=-113 time=161 busy=0 rx=3
152 * ACS: 4: min_nf=-113 interference_factor=0.0186335 nf=-113 time=161 busy=0 rx=3
153 * ACS: 5: min_nf=-113 interference_factor=0.0186335 nf=-113 time=161 busy=0 rx=3
154 * ACS: * interference factor average: 0.0148838
155 * ACS: Survey analysis for channel 4 (2427 MHz)
156 * ACS: 1: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0
157 * ACS: 2: min_nf=-114 interference_factor=0.0555556 nf=-114 time=162 busy=0 rx=9
158 * ACS: 3: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=161 busy=0 rx=0
159 * ACS: 4: min_nf=-114 interference_factor=0.0186335 nf=-114 time=161 busy=0 rx=3
160 * ACS: 5: min_nf=-114 interference_factor=0.00621118 nf=-114 time=161 busy=0 rx=1
161 * ACS: * interference factor average: 0.0160801
162 * ACS: Survey analysis for channel 5 (2432 MHz)
163 * ACS: 1: min_nf=-114 interference_factor=0.409938 nf=-113 time=161 busy=0 rx=66
164 * ACS: 2: min_nf=-114 interference_factor=0.0432099 nf=-113 time=162 busy=0 rx=7
165 * ACS: 3: min_nf=-114 interference_factor=0.0124224 nf=-113 time=161 busy=0 rx=2
166 * ACS: 4: min_nf=-114 interference_factor=0.677019 nf=-113 time=161 busy=0 rx=109
167 * ACS: 5: min_nf=-114 interference_factor=0.0186335 nf=-114 time=161 busy=0 rx=3
168 * ACS: * interference factor average: 0.232244
169 * ACS: Survey analysis for channel 6 (2437 MHz)
170 * ACS: 1: min_nf=-113 interference_factor=0.552795 nf=-113 time=161 busy=0 rx=89
171 * ACS: 2: min_nf=-113 interference_factor=0.0807453 nf=-112 time=161 busy=0 rx=13
172 * ACS: 3: min_nf=-113 interference_factor=0.0310559 nf=-113 time=161 busy=0 rx=5
173 * ACS: 4: min_nf=-113 interference_factor=0.434783 nf=-112 time=161 busy=0 rx=70
174 * ACS: 5: min_nf=-113 interference_factor=0.0621118 nf=-113 time=161 busy=0 rx=10
175 * ACS: * interference factor average: 0.232298
176 * ACS: Survey analysis for channel 7 (2442 MHz)
177 * ACS: 1: min_nf=-113 interference_factor=0.440994 nf=-112 time=161 busy=0 rx=71
178 * ACS: 2: min_nf=-113 interference_factor=0.385093 nf=-113 time=161 busy=0 rx=62
179 * ACS: 3: min_nf=-113 interference_factor=0.0372671 nf=-113 time=161 busy=0 rx=6
180 * ACS: 4: min_nf=-113 interference_factor=0.0372671 nf=-113 time=161 busy=0 rx=6
181 * ACS: 5: min_nf=-113 interference_factor=0.0745342 nf=-113 time=161 busy=0 rx=12
182 * ACS: * interference factor average: 0.195031
183 * ACS: Survey analysis for channel 8 (2447 MHz)
184 * ACS: 1: min_nf=-114 interference_factor=0.0496894 nf=-112 time=161 busy=0 rx=8
185 * ACS: 2: min_nf=-114 interference_factor=0.0496894 nf=-114 time=161 busy=0 rx=8
186 * ACS: 3: min_nf=-114 interference_factor=0.0372671 nf=-113 time=161 busy=0 rx=6
187 * ACS: 4: min_nf=-114 interference_factor=0.12963 nf=-113 time=162 busy=0 rx=21
188 * ACS: 5: min_nf=-114 interference_factor=0.166667 nf=-114 time=162 busy=0 rx=27
189 * ACS: * interference factor average: 0.0865885
190 * ACS: Survey analysis for channel 9 (2452 MHz)
191 * ACS: 1: min_nf=-114 interference_factor=0.0124224 nf=-114 time=161 busy=0 rx=2
192 * ACS: 2: min_nf=-114 interference_factor=0.0310559 nf=-114 time=161 busy=0 rx=5
193 * ACS: 3: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=161 busy=0 rx=0
194 * ACS: 4: min_nf=-114 interference_factor=0.00617284 nf=-114 time=162 busy=0 rx=1
195 * ACS: 5: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0
196 * ACS: * interference factor average: 0.00993022
197 * ACS: Survey analysis for channel 10 (2457 MHz)
198 * ACS: 1: min_nf=-114 interference_factor=0.00621118 nf=-114 time=161 busy=0 rx=1
199 * ACS: 2: min_nf=-114 interference_factor=0.00621118 nf=-114 time=161 busy=0 rx=1
200 * ACS: 3: min_nf=-114 interference_factor=0.00621118 nf=-114 time=161 busy=0 rx=1
201 * ACS: 4: min_nf=-114 interference_factor=0.0493827 nf=-114 time=162 busy=0 rx=8
202 * ACS: 5: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0
203 * ACS: * interference factor average: 0.0136033
204 * ACS: Survey analysis for channel 11 (2462 MHz)
205 * ACS: 1: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=161 busy=0 rx=0
206 * ACS: 2: min_nf=-114 interference_factor=2.51189e-23 nf=-113 time=161 busy=0 rx=0
207 * ACS: 3: min_nf=-114 interference_factor=2.51189e-23 nf=-113 time=161 busy=0 rx=0
208 * ACS: 4: min_nf=-114 interference_factor=0.0432099 nf=-114 time=162 busy=0 rx=7
209 * ACS: 5: min_nf=-114 interference_factor=0.0925926 nf=-114 time=162 busy=0 rx=15
210 * ACS: * interference factor average: 0.0271605
211 * ACS: Survey analysis for channel 12 (2467 MHz)
212 * ACS: 1: min_nf=-114 interference_factor=0.0621118 nf=-113 time=161 busy=0 rx=10
213 * ACS: 2: min_nf=-114 interference_factor=0.00621118 nf=-114 time=161 busy=0 rx=1
214 * ACS: 3: min_nf=-114 interference_factor=2.51189e-23 nf=-113 time=162 busy=0 rx=0
215 * ACS: 4: min_nf=-114 interference_factor=2.51189e-23 nf=-113 time=162 busy=0 rx=0
216 * ACS: 5: min_nf=-114 interference_factor=0.00617284 nf=-113 time=162 busy=0 rx=1
217 * ACS: * interference factor average: 0.0148992
218 * ACS: Survey analysis for channel 13 (2472 MHz)
219 * ACS: 1: min_nf=-114 interference_factor=0.0745342 nf=-114 time=161 busy=0 rx=12
220 * ACS: 2: min_nf=-114 interference_factor=0.0555556 nf=-114 time=162 busy=0 rx=9
221 * ACS: 3: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0
222 * ACS: 4: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0
223 * ACS: 5: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0
224 * ACS: * interference factor average: 0.0260179
225 * ACS: Survey analysis for selected bandwidth 20MHz
226 * ACS: * channel 1: total interference = 0.121432
227 * ACS: * channel 2: total interference = 0.137512
228 * ACS: * channel 3: total interference = 0.369757
229 * ACS: * channel 4: total interference = 0.546338
230 * ACS: * channel 5: total interference = 0.690538
231 * ACS: * channel 6: total interference = 0.762242
232 * ACS: * channel 7: total interference = 0.756092
233 * ACS: * channel 8: total interference = 0.537451
234 * ACS: * channel 9: total interference = 0.332313
235 * ACS: * channel 10: total interference = 0.152182
236 * ACS: * channel 11: total interference = 0.0916111
237 * ACS: * channel 12: total interference = 0.0816809
238 * ACS: * channel 13: total interference = 0.0680776
239 * ACS: Ideal channel is 13 (2472 MHz) with total interference factor of 0.0680776
240 *
241 * [1] http://en.wikipedia.org/wiki/Near_and_far_field
242 */
243
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000244enum bw_type {
245 ACS_BW40,
246 ACS_BW80,
247 ACS_BW160,
248};
249
250struct bw_item {
251 int first;
252 int last;
253 int center_chan;
254};
255
256static const struct bw_item bw_40[] = {
257 { 5180, 5200, 38 }, { 5220, 5240, 46 }, { 5260, 5280, 54 },
258 { 5300, 5320, 62 }, { 5500, 5520, 102 }, { 5540, 5560, 110 },
259 { 5580, 5600, 110 }, { 5620, 5640, 126}, { 5660, 5680, 134 },
260 { 5700, 5720, 142 }, { 5745, 5765, 151 }, { 5785, 5805, 159 },
261 { 5825, 5845, 167 }, { 5865, 5885, 175 },
262 { 5955, 5975, 3 }, { 5995, 6015, 11 }, { 6035, 6055, 19 },
263 { 6075, 6095, 27 }, { 6115, 6135, 35 }, { 6155, 6175, 43 },
264 { 6195, 6215, 51 }, { 6235, 6255, 59 }, { 6275, 6295, 67 },
265 { 6315, 6335, 75 }, { 6355, 6375, 83 }, { 6395, 6415, 91 },
266 { 6435, 6455, 99 }, { 6475, 6495, 107 }, { 6515, 6535, 115 },
267 { 6555, 6575, 123 }, { 6595, 6615, 131 }, { 6635, 6655, 139 },
268 { 6675, 6695, 147 }, { 6715, 6735, 155 }, { 6755, 6775, 163 },
269 { 6795, 6815, 171 }, { 6835, 6855, 179 }, { 6875, 6895, 187 },
270 { 6915, 6935, 195 }, { 6955, 6975, 203 }, { 6995, 7015, 211 },
271 { 7035, 7055, 219 }, { 7075, 7095, 227}, { -1, -1, -1 }
272};
273static const struct bw_item bw_80[] = {
274 { 5180, 5240, 42 }, { 5260, 5320, 58 }, { 5500, 5560, 106 },
275 { 5580, 5640, 122 }, { 5660, 5720, 138 }, { 5745, 5805, 155 },
276 { 5825, 5885, 171},
277 { 5955, 6015, 7 }, { 6035, 6095, 23 }, { 6115, 6175, 39 },
278 { 6195, 6255, 55 }, { 6275, 6335, 71 }, { 6355, 6415, 87 },
279 { 6435, 6495, 103 }, { 6515, 6575, 119 }, { 6595, 6655, 135 },
280 { 6675, 6735, 151 }, { 6755, 6815, 167 }, { 6835, 6895, 183 },
281 { 6915, 6975, 199 }, { 6995, 7055, 215 }, { -1, -1, -1 }
282};
283static const struct bw_item bw_160[] = {
284 { 5180, 5320, 50 }, { 5500, 5640, 114 }, { 5745, 5885, 163 },
285 { 5955, 6095, 15 }, { 6115, 6255, 47 }, { 6275, 6415, 79 },
286 { 6435, 6575, 111 }, { 6595, 6735, 143 },
287 { 6755, 6895, 175 }, { 6915, 7055, 207 }, { -1, -1, -1 }
288};
289static const struct bw_item *bw_desc[] = {
290 [ACS_BW40] = bw_40,
291 [ACS_BW80] = bw_80,
292 [ACS_BW160] = bw_160,
293};
294
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700295
296static int acs_request_scan(struct hostapd_iface *iface);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800297static int acs_survey_is_sufficient(struct freq_survey *survey);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700298
299
300static void acs_clean_chan_surveys(struct hostapd_channel_data *chan)
301{
302 struct freq_survey *survey, *tmp;
303
304 if (dl_list_empty(&chan->survey_list))
305 return;
306
307 dl_list_for_each_safe(survey, tmp, &chan->survey_list,
308 struct freq_survey, list) {
309 dl_list_del(&survey->list);
310 os_free(survey);
311 }
312}
313
314
Hai Shalomfdcde762020-04-02 11:19:20 -0700315static void acs_cleanup_mode(struct hostapd_hw_modes *mode)
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700316{
317 int i;
318 struct hostapd_channel_data *chan;
319
Hai Shalomfdcde762020-04-02 11:19:20 -0700320 for (i = 0; i < mode->num_channels; i++) {
321 chan = &mode->channels[i];
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700322
323 if (chan->flag & HOSTAPD_CHAN_SURVEY_LIST_INITIALIZED)
324 acs_clean_chan_surveys(chan);
325
326 dl_list_init(&chan->survey_list);
327 chan->flag |= HOSTAPD_CHAN_SURVEY_LIST_INITIALIZED;
328 chan->min_nf = 0;
329 }
Hai Shalomfdcde762020-04-02 11:19:20 -0700330}
331
332
333void acs_cleanup(struct hostapd_iface *iface)
334{
335 int i;
336
337 for (i = 0; i < iface->num_hw_features; i++)
338 acs_cleanup_mode(&iface->hw_features[i]);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700339
340 iface->chans_surveyed = 0;
341 iface->acs_num_completed_scans = 0;
342}
343
344
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800345static void acs_fail(struct hostapd_iface *iface)
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700346{
347 wpa_printf(MSG_ERROR, "ACS: Failed to start");
348 acs_cleanup(iface);
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -0800349 hostapd_disable_iface(iface);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700350}
351
352
353static long double
354acs_survey_interference_factor(struct freq_survey *survey, s8 min_nf)
355{
356 long double factor, busy, total;
357
358 if (survey->filled & SURVEY_HAS_CHAN_TIME_BUSY)
359 busy = survey->channel_time_busy;
360 else if (survey->filled & SURVEY_HAS_CHAN_TIME_RX)
361 busy = survey->channel_time_rx;
362 else {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700363 wpa_printf(MSG_ERROR, "ACS: Survey data missing");
364 return 0;
365 }
366
367 total = survey->channel_time;
368
369 if (survey->filled & SURVEY_HAS_CHAN_TIME_TX) {
370 busy -= survey->channel_time_tx;
371 total -= survey->channel_time_tx;
372 }
373
374 /* TODO: figure out the best multiplier for noise floor base */
375 factor = pow(10, survey->nf / 5.0L) +
Roshan Pius3a1667e2018-07-03 15:17:14 -0700376 (total ? (busy / total) : 0) *
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700377 pow(2, pow(10, (long double) survey->nf / 10.0L) -
378 pow(10, (long double) min_nf / 10.0L));
379
380 return factor;
381}
382
383
384static void
385acs_survey_chan_interference_factor(struct hostapd_iface *iface,
386 struct hostapd_channel_data *chan)
387{
388 struct freq_survey *survey;
389 unsigned int i = 0;
390 long double int_factor = 0;
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800391 unsigned count = 0;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700392
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700393 if (dl_list_empty(&chan->survey_list) ||
394 (chan->flag & HOSTAPD_CHAN_DISABLED))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700395 return;
396
397 chan->interference_factor = 0;
398
399 dl_list_for_each(survey, &chan->survey_list, struct freq_survey, list)
400 {
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800401 i++;
402
403 if (!acs_survey_is_sufficient(survey)) {
404 wpa_printf(MSG_DEBUG, "ACS: %d: insufficient data", i);
405 continue;
406 }
407
408 count++;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700409 int_factor = acs_survey_interference_factor(survey,
410 iface->lowest_nf);
411 chan->interference_factor += int_factor;
412 wpa_printf(MSG_DEBUG, "ACS: %d: min_nf=%d interference_factor=%Lg nf=%d time=%lu busy=%lu rx=%lu",
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800413 i, chan->min_nf, int_factor,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700414 survey->nf, (unsigned long) survey->channel_time,
415 (unsigned long) survey->channel_time_busy,
416 (unsigned long) survey->channel_time_rx);
417 }
418
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700419 if (count)
420 chan->interference_factor /= count;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700421}
422
423
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000424static bool acs_usable_bw_chan(const struct hostapd_channel_data *chan,
425 enum bw_type bw)
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700426{
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000427 unsigned int i = 0;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700428
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000429 while (bw_desc[bw][i].first != -1) {
430 if (chan->freq == bw_desc[bw][i].first)
431 return true;
432 i++;
433 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700434
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000435 return false;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700436}
437
438
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000439static int acs_get_bw_center_chan(int freq, enum bw_type bw)
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800440{
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000441 unsigned int i = 0;
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800442
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000443 while (bw_desc[bw][i].first != -1) {
444 if (freq >= bw_desc[bw][i].first &&
445 freq <= bw_desc[bw][i].last)
446 return bw_desc[bw][i].center_chan;
447 i++;
448 }
Hai Shalom74f70d42019-02-11 14:42:39 -0800449
450 return 0;
451}
452
453
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700454static int acs_survey_is_sufficient(struct freq_survey *survey)
455{
456 if (!(survey->filled & SURVEY_HAS_NF)) {
Sunil Ravi89eba102022-09-13 21:04:37 -0700457 wpa_printf(MSG_INFO,
458 "ACS: Survey for freq %d is missing noise floor",
459 survey->freq);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700460 return 0;
461 }
462
463 if (!(survey->filled & SURVEY_HAS_CHAN_TIME)) {
Sunil Ravi89eba102022-09-13 21:04:37 -0700464 wpa_printf(MSG_INFO,
465 "ACS: Survey for freq %d is missing channel time",
466 survey->freq);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700467 return 0;
468 }
469
470 if (!(survey->filled & SURVEY_HAS_CHAN_TIME_BUSY) &&
471 !(survey->filled & SURVEY_HAS_CHAN_TIME_RX)) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800472 wpa_printf(MSG_INFO,
Sunil Ravi89eba102022-09-13 21:04:37 -0700473 "ACS: Survey for freq %d is missing RX and busy time (at least one is required)",
474 survey->freq);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700475 return 0;
476 }
477
478 return 1;
479}
480
481
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700482static int acs_survey_list_is_sufficient(struct hostapd_channel_data *chan)
483{
484 struct freq_survey *survey;
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800485 int ret = -1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700486
487 dl_list_for_each(survey, &chan->survey_list, struct freq_survey, list)
488 {
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800489 if (acs_survey_is_sufficient(survey)) {
490 ret = 1;
491 break;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700492 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800493 ret = 0;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700494 }
495
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800496 if (ret == -1)
497 ret = 1; /* no survey list entries */
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700498
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800499 if (!ret) {
500 wpa_printf(MSG_INFO,
501 "ACS: Channel %d has insufficient survey data",
502 chan->chan);
503 }
504
505 return ret;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700506}
507
508
Hai Shalomfdcde762020-04-02 11:19:20 -0700509static int acs_surveys_are_sufficient_mode(struct hostapd_hw_modes *mode)
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700510{
511 int i;
512 struct hostapd_channel_data *chan;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700513
Hai Shalomfdcde762020-04-02 11:19:20 -0700514 for (i = 0; i < mode->num_channels; i++) {
515 chan = &mode->channels[i];
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700516 if (!(chan->flag & HOSTAPD_CHAN_DISABLED) &&
517 acs_survey_list_is_sufficient(chan))
Hai Shalomfdcde762020-04-02 11:19:20 -0700518 return 1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700519 }
520
Hai Shalomfdcde762020-04-02 11:19:20 -0700521 return 0;
522}
523
524
525static int acs_surveys_are_sufficient(struct hostapd_iface *iface)
526{
527 int i;
528 struct hostapd_hw_modes *mode;
529
530 for (i = 0; i < iface->num_hw_features; i++) {
531 mode = &iface->hw_features[i];
532 if (!hostapd_hw_skip_mode(iface, mode) &&
533 acs_surveys_are_sufficient_mode(mode))
534 return 1;
535 }
536
537 return 0;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700538}
539
540
541static int acs_usable_chan(struct hostapd_channel_data *chan)
542{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700543 return !dl_list_empty(&chan->survey_list) &&
544 !(chan->flag & HOSTAPD_CHAN_DISABLED) &&
545 acs_survey_list_is_sufficient(chan);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700546}
547
548
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800549static int is_in_chanlist(struct hostapd_iface *iface,
550 struct hostapd_channel_data *chan)
551{
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700552 if (!iface->conf->acs_ch_list.num)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800553 return 1;
554
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700555 return freq_range_list_includes(&iface->conf->acs_ch_list, chan->chan);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800556}
557
558
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700559static int is_in_freqlist(struct hostapd_iface *iface,
560 struct hostapd_channel_data *chan)
561{
562 if (!iface->conf->acs_freq_list.num)
563 return 1;
564
565 return freq_range_list_includes(&iface->conf->acs_freq_list,
566 chan->freq);
567}
568
569
Hai Shalomfdcde762020-04-02 11:19:20 -0700570static void acs_survey_mode_interference_factor(
571 struct hostapd_iface *iface, struct hostapd_hw_modes *mode)
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700572{
573 int i;
574 struct hostapd_channel_data *chan;
575
Hai Shalomfdcde762020-04-02 11:19:20 -0700576 for (i = 0; i < mode->num_channels; i++) {
577 chan = &mode->channels[i];
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700578
579 if (!acs_usable_chan(chan))
580 continue;
581
Sunil Ravia04bd252022-05-02 22:54:18 -0700582 if ((chan->flag & HOSTAPD_CHAN_RADAR) &&
583 iface->conf->acs_exclude_dfs)
584 continue;
585
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800586 if (!is_in_chanlist(iface, chan))
587 continue;
588
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700589 if (!is_in_freqlist(iface, chan))
590 continue;
591
Hai Shaloma20dcd72022-02-04 13:43:00 -0800592 if (chan->max_tx_power < iface->conf->min_tx_power)
593 continue;
594
Sunil Ravi89eba102022-09-13 21:04:37 -0700595 if ((chan->flag & HOSTAPD_CHAN_INDOOR_ONLY) &&
596 iface->conf->country[2] == 0x4f)
597 continue;
598
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700599 wpa_printf(MSG_DEBUG, "ACS: Survey analysis for channel %d (%d MHz)",
600 chan->chan, chan->freq);
601
602 acs_survey_chan_interference_factor(iface, chan);
603
604 wpa_printf(MSG_DEBUG, "ACS: * interference factor average: %Lg",
605 chan->interference_factor);
606 }
607}
608
609
Hai Shalomfdcde762020-04-02 11:19:20 -0700610static void acs_survey_all_chans_interference_factor(
611 struct hostapd_iface *iface)
612{
613 int i;
614 struct hostapd_hw_modes *mode;
615
616 for (i = 0; i < iface->num_hw_features; i++) {
617 mode = &iface->hw_features[i];
618 if (!hostapd_hw_skip_mode(iface, mode))
619 acs_survey_mode_interference_factor(iface, mode);
620 }
621}
622
623
624static struct hostapd_channel_data *
625acs_find_chan_mode(struct hostapd_hw_modes *mode, int freq)
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700626{
627 struct hostapd_channel_data *chan;
628 int i;
629
Hai Shalomfdcde762020-04-02 11:19:20 -0700630 for (i = 0; i < mode->num_channels; i++) {
631 chan = &mode->channels[i];
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700632
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700633 if (chan->flag & HOSTAPD_CHAN_DISABLED)
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700634 continue;
635
636 if (chan->freq == freq)
637 return chan;
638 }
639
640 return NULL;
641}
642
643
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000644static enum hostapd_hw_mode
645acs_find_mode(struct hostapd_iface *iface, int freq)
646{
647 int i;
648 struct hostapd_hw_modes *mode;
649 struct hostapd_channel_data *chan;
650
651 for (i = 0; i < iface->num_hw_features; i++) {
652 mode = &iface->hw_features[i];
653 if (!hostapd_hw_skip_mode(iface, mode)) {
654 chan = acs_find_chan_mode(mode, freq);
655 if (chan)
656 return mode->mode;
657 }
658 }
659
660 return HOSTAPD_MODE_IEEE80211ANY;
661}
662
663
Hai Shalomfdcde762020-04-02 11:19:20 -0700664static struct hostapd_channel_data *
665acs_find_chan(struct hostapd_iface *iface, int freq)
666{
667 int i;
668 struct hostapd_hw_modes *mode;
669 struct hostapd_channel_data *chan;
670
671 for (i = 0; i < iface->num_hw_features; i++) {
672 mode = &iface->hw_features[i];
673 if (!hostapd_hw_skip_mode(iface, mode)) {
674 chan = acs_find_chan_mode(mode, freq);
675 if (chan)
676 return chan;
677 }
678 }
679
680 return NULL;
681}
682
683
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800684static int is_24ghz_mode(enum hostapd_hw_mode mode)
685{
686 return mode == HOSTAPD_MODE_IEEE80211B ||
687 mode == HOSTAPD_MODE_IEEE80211G;
688}
689
690
691static int is_common_24ghz_chan(int chan)
692{
693 return chan == 1 || chan == 6 || chan == 11;
694}
695
696
697#ifndef ACS_ADJ_WEIGHT
698#define ACS_ADJ_WEIGHT 0.85
699#endif /* ACS_ADJ_WEIGHT */
700
701#ifndef ACS_NEXT_ADJ_WEIGHT
702#define ACS_NEXT_ADJ_WEIGHT 0.55
703#endif /* ACS_NEXT_ADJ_WEIGHT */
704
705#ifndef ACS_24GHZ_PREFER_1_6_11
706/*
707 * Select commonly used channels 1, 6, 11 by default even if a neighboring
708 * channel has a smaller interference factor as long as it is not better by more
709 * than this multiplier.
710 */
711#define ACS_24GHZ_PREFER_1_6_11 0.8
712#endif /* ACS_24GHZ_PREFER_1_6_11 */
713
Hai Shalomfdcde762020-04-02 11:19:20 -0700714static void
715acs_find_ideal_chan_mode(struct hostapd_iface *iface,
716 struct hostapd_hw_modes *mode,
717 int n_chans, u32 bw,
718 struct hostapd_channel_data **rand_chan,
719 struct hostapd_channel_data **ideal_chan,
720 long double *ideal_factor)
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700721{
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000722 struct hostapd_channel_data *chan, *adj_chan = NULL, *best;
Hai Shalomfdcde762020-04-02 11:19:20 -0700723 long double factor;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700724 int i, j;
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800725 unsigned int k;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700726
Hai Shalomfdcde762020-04-02 11:19:20 -0700727 for (i = 0; i < mode->num_channels; i++) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800728 double total_weight;
729 struct acs_bias *bias, tmp_bias;
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000730 bool update_best = true;
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800731
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000732 best = chan = &mode->channels[i];
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700733
Hai Shalom74f70d42019-02-11 14:42:39 -0800734 /* Since in the current ACS implementation the first channel is
735 * always a primary channel, skip channels not available as
736 * primary until more sophisticated channel selection is
737 * implemented. */
738 if (!chan_pri_allowed(chan))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700739 continue;
740
Sunil Ravia04bd252022-05-02 22:54:18 -0700741 if ((chan->flag & HOSTAPD_CHAN_RADAR) &&
742 iface->conf->acs_exclude_dfs)
743 continue;
744
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800745 if (!is_in_chanlist(iface, chan))
746 continue;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700747
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700748 if (!is_in_freqlist(iface, chan))
749 continue;
750
Hai Shaloma20dcd72022-02-04 13:43:00 -0800751 if (chan->max_tx_power < iface->conf->min_tx_power)
752 continue;
753
Sunil Ravi89eba102022-09-13 21:04:37 -0700754 if ((chan->flag & HOSTAPD_CHAN_INDOOR_ONLY) &&
755 iface->conf->country[2] == 0x4f)
756 continue;
757
Hai Shalom74f70d42019-02-11 14:42:39 -0800758 if (!chan_bw_allowed(chan, bw, 1, 1)) {
759 wpa_printf(MSG_DEBUG,
760 "ACS: Channel %d: BW %u is not supported",
761 chan->chan, bw);
762 continue;
763 }
764
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700765 /* HT40 on 5 GHz has a limited set of primary channels as per
766 * 11n Annex J */
Hai Shalomfdcde762020-04-02 11:19:20 -0700767 if (mode->mode == HOSTAPD_MODE_IEEE80211A &&
Hai Shaloma20dcd72022-02-04 13:43:00 -0800768 ((iface->conf->ieee80211n &&
769 iface->conf->secondary_channel) ||
770 is_6ghz_freq(chan->freq)) &&
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000771 !acs_usable_bw_chan(chan, ACS_BW40)) {
Hai Shaloma20dcd72022-02-04 13:43:00 -0800772 wpa_printf(MSG_DEBUG,
773 "ACS: Channel %d: not allowed as primary channel for 40 MHz bandwidth",
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700774 chan->chan);
775 continue;
776 }
777
Hai Shalomfdcde762020-04-02 11:19:20 -0700778 if (mode->mode == HOSTAPD_MODE_IEEE80211A &&
Hai Shalom81f62d82019-07-22 12:10:00 -0700779 (iface->conf->ieee80211ac || iface->conf->ieee80211ax)) {
780 if (hostapd_get_oper_chwidth(iface->conf) ==
Sunil8cd6f4d2022-06-28 18:40:46 +0000781 CONF_OPER_CHWIDTH_80MHZ &&
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000782 !acs_usable_bw_chan(chan, ACS_BW80)) {
Hai Shalom74f70d42019-02-11 14:42:39 -0800783 wpa_printf(MSG_DEBUG,
Hai Shaloma20dcd72022-02-04 13:43:00 -0800784 "ACS: Channel %d: not allowed as primary channel for 80 MHz bandwidth",
Hai Shalom74f70d42019-02-11 14:42:39 -0800785 chan->chan);
786 continue;
787 }
788
Hai Shalom81f62d82019-07-22 12:10:00 -0700789 if (hostapd_get_oper_chwidth(iface->conf) ==
Sunil8cd6f4d2022-06-28 18:40:46 +0000790 CONF_OPER_CHWIDTH_160MHZ &&
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000791 !acs_usable_bw_chan(chan, ACS_BW160)) {
Hai Shalom74f70d42019-02-11 14:42:39 -0800792 wpa_printf(MSG_DEBUG,
Hai Shaloma20dcd72022-02-04 13:43:00 -0800793 "ACS: Channel %d: not allowed as primary channel for 160 MHz bandwidth",
Hai Shalom74f70d42019-02-11 14:42:39 -0800794 chan->chan);
795 continue;
796 }
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800797 }
798
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700799 factor = 0;
800 if (acs_usable_chan(chan))
801 factor = chan->interference_factor;
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800802 total_weight = 1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700803
804 for (j = 1; j < n_chans; j++) {
805 adj_chan = acs_find_chan(iface, chan->freq + (j * 20));
806 if (!adj_chan)
807 break;
808
Hai Shalom74f70d42019-02-11 14:42:39 -0800809 if (!chan_bw_allowed(adj_chan, bw, 1, 0)) {
810 wpa_printf(MSG_DEBUG,
811 "ACS: PRI Channel %d: secondary channel %d BW %u is not supported",
812 chan->chan, adj_chan->chan, bw);
813 break;
814 }
815
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800816 if (acs_usable_chan(adj_chan)) {
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700817 factor += adj_chan->interference_factor;
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800818 total_weight += 1;
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000819 } else {
820 update_best = false;
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800821 }
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000822
823 /* find the best channel in this segment */
824 if (update_best &&
825 adj_chan->interference_factor <
826 best->interference_factor)
827 best = adj_chan;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700828 }
829
830 if (j != n_chans) {
831 wpa_printf(MSG_DEBUG, "ACS: Channel %d: not enough bandwidth",
832 chan->chan);
833 continue;
834 }
835
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000836 /* If the AP is in the 5 GHz or 6 GHz band, lets prefer a less
837 * crowded primary channel if one was found in the segment */
838 if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A &&
839 chan != best) {
840 wpa_printf(MSG_DEBUG,
841 "ACS: promoting channel %d over %d (less interference %Lg/%Lg)",
842 best->chan, chan->chan,
843 chan->interference_factor,
844 best->interference_factor);
845 chan = best;
846 }
847
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700848 /* 2.4 GHz has overlapping 20 MHz channels. Include adjacent
849 * channel interference factor. */
Hai Shalomfdcde762020-04-02 11:19:20 -0700850 if (is_24ghz_mode(mode->mode)) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700851 for (j = 0; j < n_chans; j++) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700852 adj_chan = acs_find_chan(iface, chan->freq +
853 (j * 20) - 5);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800854 if (adj_chan && acs_usable_chan(adj_chan)) {
855 factor += ACS_ADJ_WEIGHT *
856 adj_chan->interference_factor;
857 total_weight += ACS_ADJ_WEIGHT;
858 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700859
860 adj_chan = acs_find_chan(iface, chan->freq +
861 (j * 20) - 10);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800862 if (adj_chan && acs_usable_chan(adj_chan)) {
863 factor += ACS_NEXT_ADJ_WEIGHT *
864 adj_chan->interference_factor;
865 total_weight += ACS_NEXT_ADJ_WEIGHT;
866 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700867
868 adj_chan = acs_find_chan(iface, chan->freq +
869 (j * 20) + 5);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800870 if (adj_chan && acs_usable_chan(adj_chan)) {
871 factor += ACS_ADJ_WEIGHT *
872 adj_chan->interference_factor;
873 total_weight += ACS_ADJ_WEIGHT;
874 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700875
876 adj_chan = acs_find_chan(iface, chan->freq +
877 (j * 20) + 10);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800878 if (adj_chan && acs_usable_chan(adj_chan)) {
879 factor += ACS_NEXT_ADJ_WEIGHT *
880 adj_chan->interference_factor;
881 total_weight += ACS_NEXT_ADJ_WEIGHT;
882 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700883 }
884 }
885
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800886 factor /= total_weight;
887
888 bias = NULL;
889 if (iface->conf->acs_chan_bias) {
890 for (k = 0; k < iface->conf->num_acs_chan_bias; k++) {
891 bias = &iface->conf->acs_chan_bias[k];
892 if (bias->channel == chan->chan)
893 break;
894 bias = NULL;
895 }
Hai Shalomfdcde762020-04-02 11:19:20 -0700896 } else if (is_24ghz_mode(mode->mode) &&
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800897 is_common_24ghz_chan(chan->chan)) {
898 tmp_bias.channel = chan->chan;
899 tmp_bias.bias = ACS_24GHZ_PREFER_1_6_11;
900 bias = &tmp_bias;
901 }
902
903 if (bias) {
904 factor *= bias->bias;
905 wpa_printf(MSG_DEBUG,
906 "ACS: * channel %d: total interference = %Lg (%f bias)",
907 chan->chan, factor, bias->bias);
908 } else {
909 wpa_printf(MSG_DEBUG,
910 "ACS: * channel %d: total interference = %Lg",
911 chan->chan, factor);
912 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700913
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700914 if (acs_usable_chan(chan) &&
Hai Shalomfdcde762020-04-02 11:19:20 -0700915 (!*ideal_chan || factor < *ideal_factor)) {
916 *ideal_factor = factor;
917 *ideal_chan = chan;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700918 }
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700919
920 /* This channel would at least be usable */
Hai Shalomfdcde762020-04-02 11:19:20 -0700921 if (!(*rand_chan))
922 *rand_chan = chan;
923 }
924}
925
926
927/*
928 * At this point it's assumed chan->interference_factor has been computed.
929 * This function should be reusable regardless of interference computation
930 * option (survey, BSS, spectral, ...). chan->interference factor must be
931 * summable (i.e., must be always greater than zero).
932 */
933static struct hostapd_channel_data *
934acs_find_ideal_chan(struct hostapd_iface *iface)
935{
936 struct hostapd_channel_data *ideal_chan = NULL,
937 *rand_chan = NULL;
938 long double ideal_factor = 0;
939 int i;
940 int n_chans = 1;
941 u32 bw;
942 struct hostapd_hw_modes *mode;
943
Hai Shaloma20dcd72022-02-04 13:43:00 -0800944 if (is_6ghz_op_class(iface->conf->op_class)) {
945 bw = op_class_to_bandwidth(iface->conf->op_class);
946 n_chans = bw / 20;
947 goto bw_selected;
948 }
949
Hai Shalomfdcde762020-04-02 11:19:20 -0700950 /* TODO: HT40- support */
951
952 if (iface->conf->ieee80211n &&
953 iface->conf->secondary_channel == -1) {
954 wpa_printf(MSG_ERROR, "ACS: HT40- is not supported yet. Please try HT40+");
955 return NULL;
956 }
957
958 if (iface->conf->ieee80211n &&
959 iface->conf->secondary_channel)
960 n_chans = 2;
961
962 if (iface->conf->ieee80211ac || iface->conf->ieee80211ax) {
963 switch (hostapd_get_oper_chwidth(iface->conf)) {
Sunil8cd6f4d2022-06-28 18:40:46 +0000964 case CONF_OPER_CHWIDTH_80MHZ:
Hai Shalomfdcde762020-04-02 11:19:20 -0700965 n_chans = 4;
966 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000967 case CONF_OPER_CHWIDTH_160MHZ:
Hai Shalomfdcde762020-04-02 11:19:20 -0700968 n_chans = 8;
969 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000970 default:
971 break;
Hai Shalomfdcde762020-04-02 11:19:20 -0700972 }
973 }
974
975 bw = num_chan_to_bw(n_chans);
976
Hai Shaloma20dcd72022-02-04 13:43:00 -0800977bw_selected:
Hai Shalomfdcde762020-04-02 11:19:20 -0700978 /* TODO: VHT/HE80+80. Update acs_adjust_center_freq() too. */
979
980 wpa_printf(MSG_DEBUG,
981 "ACS: Survey analysis for selected bandwidth %d MHz", bw);
982
983 for (i = 0; i < iface->num_hw_features; i++) {
984 mode = &iface->hw_features[i];
985 if (!hostapd_hw_skip_mode(iface, mode))
986 acs_find_ideal_chan_mode(iface, mode, n_chans, bw,
987 &rand_chan, &ideal_chan,
988 &ideal_factor);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700989 }
990
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700991 if (ideal_chan) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700992 wpa_printf(MSG_DEBUG, "ACS: Ideal channel is %d (%d MHz) with total interference factor of %Lg",
993 ideal_chan->chan, ideal_chan->freq, ideal_factor);
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700994 return ideal_chan;
995 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700996
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700997 return rand_chan;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700998}
999
1000
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001001static void acs_adjust_secondary(struct hostapd_iface *iface)
1002{
1003 unsigned int i;
1004
1005 /* When working with bandwidth over 20 MHz on the 5 GHz or 6 GHz band,
1006 * ACS can return a secondary channel which is not the first channel of
1007 * the segment and we need to adjust. */
1008 if (!iface->conf->secondary_channel ||
1009 acs_find_mode(iface, iface->freq) != HOSTAPD_MODE_IEEE80211A)
1010 return;
1011
1012 wpa_printf(MSG_DEBUG, "ACS: Adjusting HT/VHT/HE secondary frequency");
1013
1014 for (i = 0; bw_desc[ACS_BW40][i].first != -1; i++) {
1015 if (iface->freq == bw_desc[ACS_BW40][i].first)
1016 iface->conf->secondary_channel = 1;
1017 else if (iface->freq == bw_desc[ACS_BW40][i].last)
1018 iface->conf->secondary_channel = -1;
1019 }
1020}
1021
1022
Hai Shalom81f62d82019-07-22 12:10:00 -07001023static void acs_adjust_center_freq(struct hostapd_iface *iface)
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001024{
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001025 int center;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001026
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001027 wpa_printf(MSG_DEBUG, "ACS: Adjusting VHT center frequency");
1028
Hai Shalom81f62d82019-07-22 12:10:00 -07001029 switch (hostapd_get_oper_chwidth(iface->conf)) {
Sunil8cd6f4d2022-06-28 18:40:46 +00001030 case CONF_OPER_CHWIDTH_USE_HT:
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001031 if (iface->conf->secondary_channel &&
1032 iface->freq >= 2400 && iface->freq < 2500)
1033 center = iface->conf->channel +
1034 2 * iface->conf->secondary_channel;
1035 else if (iface->conf->secondary_channel)
1036 center = acs_get_bw_center_chan(iface->freq, ACS_BW40);
1037 else
1038 center = iface->conf->channel;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001039 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00001040 case CONF_OPER_CHWIDTH_80MHZ:
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001041 center = acs_get_bw_center_chan(iface->freq, ACS_BW80);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001042 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00001043 case CONF_OPER_CHWIDTH_160MHZ:
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001044 center = acs_get_bw_center_chan(iface->freq, ACS_BW160);
Hai Shalom74f70d42019-02-11 14:42:39 -08001045 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001046 default:
1047 /* TODO: How can this be calculated? Adjust
1048 * acs_find_ideal_chan() */
Hai Shalom74f70d42019-02-11 14:42:39 -08001049 wpa_printf(MSG_INFO,
1050 "ACS: Only VHT20/40/80/160 is supported now");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001051 return;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001052 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001053
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001054 hostapd_set_oper_centr_freq_seg0_idx(iface->conf, center);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001055}
1056
1057
1058static int acs_study_survey_based(struct hostapd_iface *iface)
1059{
1060 wpa_printf(MSG_DEBUG, "ACS: Trying survey-based ACS");
1061
1062 if (!iface->chans_surveyed) {
1063 wpa_printf(MSG_ERROR, "ACS: Unable to collect survey data");
1064 return -1;
1065 }
1066
1067 if (!acs_surveys_are_sufficient(iface)) {
1068 wpa_printf(MSG_ERROR, "ACS: Surveys have insufficient data");
1069 return -1;
1070 }
1071
Hai Shalomfdcde762020-04-02 11:19:20 -07001072 acs_survey_all_chans_interference_factor(iface);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001073 return 0;
1074}
1075
1076
1077static int acs_study_options(struct hostapd_iface *iface)
1078{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001079 if (acs_study_survey_based(iface) == 0)
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001080 return 0;
1081
1082 /* TODO: If no surveys are available/sufficient this is a good
1083 * place to fallback to BSS-based ACS */
1084
1085 return -1;
1086}
1087
1088
1089static void acs_study(struct hostapd_iface *iface)
1090{
1091 struct hostapd_channel_data *ideal_chan;
1092 int err;
1093
1094 err = acs_study_options(iface);
1095 if (err < 0) {
1096 wpa_printf(MSG_ERROR, "ACS: All study options have failed");
1097 goto fail;
1098 }
1099
1100 ideal_chan = acs_find_ideal_chan(iface);
1101 if (!ideal_chan) {
1102 wpa_printf(MSG_ERROR, "ACS: Failed to compute ideal channel");
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07001103 err = -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001104 goto fail;
1105 }
1106
1107 iface->conf->channel = ideal_chan->chan;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001108 iface->freq = ideal_chan->freq;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001109
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001110 if (iface->conf->ieee80211ac || iface->conf->ieee80211ax) {
1111 acs_adjust_secondary(iface);
Hai Shalom81f62d82019-07-22 12:10:00 -07001112 acs_adjust_center_freq(iface);
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001113 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001114
Sunil Ravi3e88b0e2022-08-10 22:56:55 +00001115 err = hostapd_select_hw_mode(iface);
1116 if (err) {
Sunil Ravi89eba102022-09-13 21:04:37 -07001117 wpa_printf(MSG_ERROR,
1118 "ACS: Could not (err: %d) select hw_mode for freq=%d channel=%d",
Sunil Ravi3e88b0e2022-08-10 22:56:55 +00001119 err, iface->freq, iface->conf->channel);
1120 err = -1;
1121 goto fail;
1122 }
1123
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07001124 err = 0;
1125fail:
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001126 /*
1127 * hostapd_setup_interface_complete() will return -1 on failure,
1128 * 0 on success and 0 is HOSTAPD_CHAN_VALID :)
1129 */
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07001130 if (hostapd_acs_completed(iface, err) == HOSTAPD_CHAN_VALID) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001131 acs_cleanup(iface);
1132 return;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001133 }
1134
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07001135 /* This can possibly happen if channel parameters (secondary
1136 * channel, center frequencies) are misconfigured */
1137 wpa_printf(MSG_ERROR, "ACS: Possibly channel configuration is invalid, please report this along with your config file.");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001138 acs_fail(iface);
1139}
1140
1141
1142static void acs_scan_complete(struct hostapd_iface *iface)
1143{
1144 int err;
1145
1146 iface->scan_cb = NULL;
1147
1148 wpa_printf(MSG_DEBUG, "ACS: Using survey based algorithm (acs_num_scans=%d)",
1149 iface->conf->acs_num_scans);
1150
1151 err = hostapd_drv_get_survey(iface->bss[0], 0);
1152 if (err) {
1153 wpa_printf(MSG_ERROR, "ACS: Failed to get survey data");
Dmitry Shmidt15907092014-03-25 10:42:57 -07001154 goto fail;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001155 }
1156
1157 if (++iface->acs_num_completed_scans < iface->conf->acs_num_scans) {
1158 err = acs_request_scan(iface);
1159 if (err) {
1160 wpa_printf(MSG_ERROR, "ACS: Failed to request scan");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001161 goto fail;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001162 }
1163
1164 return;
1165 }
1166
1167 acs_study(iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001168 return;
1169fail:
1170 hostapd_acs_completed(iface, 1);
1171 acs_fail(iface);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001172}
1173
1174
Hai Shalomfdcde762020-04-02 11:19:20 -07001175static int * acs_request_scan_add_freqs(struct hostapd_iface *iface,
1176 struct hostapd_hw_modes *mode,
1177 int *freq)
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001178{
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001179 struct hostapd_channel_data *chan;
Hai Shalomfdcde762020-04-02 11:19:20 -07001180 int i;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001181
Hai Shalomfdcde762020-04-02 11:19:20 -07001182 for (i = 0; i < mode->num_channels; i++) {
1183 chan = &mode->channels[i];
Sunil Ravia04bd252022-05-02 22:54:18 -07001184 if ((chan->flag & HOSTAPD_CHAN_DISABLED) ||
1185 ((chan->flag & HOSTAPD_CHAN_RADAR) &&
1186 iface->conf->acs_exclude_dfs))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001187 continue;
1188
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001189 if (!is_in_chanlist(iface, chan))
1190 continue;
1191
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001192 if (!is_in_freqlist(iface, chan))
1193 continue;
1194
Hai Shaloma20dcd72022-02-04 13:43:00 -08001195 if (chan->max_tx_power < iface->conf->min_tx_power)
1196 continue;
1197
Sunil Ravi89eba102022-09-13 21:04:37 -07001198 if ((chan->flag & HOSTAPD_CHAN_INDOOR_ONLY) &&
1199 iface->conf->country[2] == 0x4f)
1200 continue;
1201
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001202 *freq++ = chan->freq;
1203 }
Hai Shalomfdcde762020-04-02 11:19:20 -07001204
1205 return freq;
1206}
1207
1208
1209static int acs_request_scan(struct hostapd_iface *iface)
1210{
1211 struct wpa_driver_scan_params params;
1212 int i, *freq;
1213 int num_channels;
1214 struct hostapd_hw_modes *mode;
1215
1216 os_memset(&params, 0, sizeof(params));
1217
1218 num_channels = 0;
1219 for (i = 0; i < iface->num_hw_features; i++) {
1220 mode = &iface->hw_features[i];
1221 if (!hostapd_hw_skip_mode(iface, mode))
1222 num_channels += mode->num_channels;
1223 }
1224
1225 params.freqs = os_calloc(num_channels + 1, sizeof(params.freqs[0]));
1226 if (params.freqs == NULL)
1227 return -1;
1228
1229 freq = params.freqs;
1230
1231 for (i = 0; i < iface->num_hw_features; i++) {
1232 mode = &iface->hw_features[i];
1233 if (!hostapd_hw_skip_mode(iface, mode))
1234 freq = acs_request_scan_add_freqs(iface, mode, freq);
1235 }
1236
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001237 *freq = 0;
1238
Neo Joue2881702019-10-17 11:32:28 +08001239 if (params.freqs == freq) {
1240 wpa_printf(MSG_ERROR, "ACS: No available channels found");
1241 os_free(params.freqs);
1242 return -1;
1243 }
1244
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001245 iface->scan_cb = acs_scan_complete;
1246
1247 wpa_printf(MSG_DEBUG, "ACS: Scanning %d / %d",
1248 iface->acs_num_completed_scans + 1,
1249 iface->conf->acs_num_scans);
1250
1251 if (hostapd_driver_scan(iface->bss[0], &params) < 0) {
1252 wpa_printf(MSG_ERROR, "ACS: Failed to request initial scan");
1253 acs_cleanup(iface);
Dmitry Shmidt15907092014-03-25 10:42:57 -07001254 os_free(params.freqs);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001255 return -1;
1256 }
1257
1258 os_free(params.freqs);
1259 return 0;
1260}
1261
1262
1263enum hostapd_chan_status acs_init(struct hostapd_iface *iface)
1264{
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001265 wpa_printf(MSG_INFO, "ACS: Automatic channel selection started, this may take a bit");
1266
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001267 if (iface->drv_flags & WPA_DRIVER_FLAGS_ACS_OFFLOAD) {
1268 wpa_printf(MSG_INFO, "ACS: Offloading to driver");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001269 if (hostapd_drv_do_acs(iface->bss[0]))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001270 return HOSTAPD_CHAN_INVALID;
1271 return HOSTAPD_CHAN_ACS;
1272 }
1273
Hai Shalomfdcde762020-04-02 11:19:20 -07001274 if (!iface->current_mode &&
1275 iface->conf->hw_mode != HOSTAPD_MODE_IEEE80211ANY)
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001276 return HOSTAPD_CHAN_INVALID;
1277
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001278 acs_cleanup(iface);
1279
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001280 if (acs_request_scan(iface) < 0)
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001281 return HOSTAPD_CHAN_INVALID;
1282
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001283 hostapd_set_state(iface, HAPD_IFACE_ACS);
1284 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, ACS_EVENT_STARTED);
1285
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001286 return HOSTAPD_CHAN_ACS;
1287}