blob: 28b0ba71cc7b50360d218c7e6a06db2861c8daa0 [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,
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000248 ACS_BW320_1,
249 ACS_BW320_2,
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000250};
251
252struct bw_item {
253 int first;
254 int last;
255 int center_chan;
256};
257
258static const struct bw_item bw_40[] = {
259 { 5180, 5200, 38 }, { 5220, 5240, 46 }, { 5260, 5280, 54 },
260 { 5300, 5320, 62 }, { 5500, 5520, 102 }, { 5540, 5560, 110 },
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000261 { 5580, 5600, 118 }, { 5620, 5640, 126 }, { 5660, 5680, 134 },
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000262 { 5700, 5720, 142 }, { 5745, 5765, 151 }, { 5785, 5805, 159 },
263 { 5825, 5845, 167 }, { 5865, 5885, 175 },
264 { 5955, 5975, 3 }, { 5995, 6015, 11 }, { 6035, 6055, 19 },
265 { 6075, 6095, 27 }, { 6115, 6135, 35 }, { 6155, 6175, 43 },
266 { 6195, 6215, 51 }, { 6235, 6255, 59 }, { 6275, 6295, 67 },
267 { 6315, 6335, 75 }, { 6355, 6375, 83 }, { 6395, 6415, 91 },
268 { 6435, 6455, 99 }, { 6475, 6495, 107 }, { 6515, 6535, 115 },
269 { 6555, 6575, 123 }, { 6595, 6615, 131 }, { 6635, 6655, 139 },
270 { 6675, 6695, 147 }, { 6715, 6735, 155 }, { 6755, 6775, 163 },
271 { 6795, 6815, 171 }, { 6835, 6855, 179 }, { 6875, 6895, 187 },
272 { 6915, 6935, 195 }, { 6955, 6975, 203 }, { 6995, 7015, 211 },
273 { 7035, 7055, 219 }, { 7075, 7095, 227}, { -1, -1, -1 }
274};
275static const struct bw_item bw_80[] = {
276 { 5180, 5240, 42 }, { 5260, 5320, 58 }, { 5500, 5560, 106 },
277 { 5580, 5640, 122 }, { 5660, 5720, 138 }, { 5745, 5805, 155 },
278 { 5825, 5885, 171},
279 { 5955, 6015, 7 }, { 6035, 6095, 23 }, { 6115, 6175, 39 },
280 { 6195, 6255, 55 }, { 6275, 6335, 71 }, { 6355, 6415, 87 },
281 { 6435, 6495, 103 }, { 6515, 6575, 119 }, { 6595, 6655, 135 },
282 { 6675, 6735, 151 }, { 6755, 6815, 167 }, { 6835, 6895, 183 },
283 { 6915, 6975, 199 }, { 6995, 7055, 215 }, { -1, -1, -1 }
284};
285static const struct bw_item bw_160[] = {
286 { 5180, 5320, 50 }, { 5500, 5640, 114 }, { 5745, 5885, 163 },
287 { 5955, 6095, 15 }, { 6115, 6255, 47 }, { 6275, 6415, 79 },
288 { 6435, 6575, 111 }, { 6595, 6735, 143 },
289 { 6755, 6895, 175 }, { 6915, 7055, 207 }, { -1, -1, -1 }
290};
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000291static const struct bw_item bw_320_1[] = {
292 { 5955, 6255, 31 }, { 6275, 6575, 95 }, { 6595, 6895, 159 },
293 { -1, -1, -1 }
294};
295static const struct bw_item bw_320_2[] = {
296 { 6115, 6415, 63 }, { 6435, 6735, 127 }, { 6755, 7055, 191 },
297 { -1, -1, -1 }
298};
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000299static const struct bw_item *bw_desc[] = {
300 [ACS_BW40] = bw_40,
301 [ACS_BW80] = bw_80,
302 [ACS_BW160] = bw_160,
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000303 [ACS_BW320_1] = bw_320_1,
304 [ACS_BW320_2] = bw_320_2,
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000305};
306
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700307
308static int acs_request_scan(struct hostapd_iface *iface);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800309static int acs_survey_is_sufficient(struct freq_survey *survey);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700310
311
312static void acs_clean_chan_surveys(struct hostapd_channel_data *chan)
313{
314 struct freq_survey *survey, *tmp;
315
316 if (dl_list_empty(&chan->survey_list))
317 return;
318
319 dl_list_for_each_safe(survey, tmp, &chan->survey_list,
320 struct freq_survey, list) {
321 dl_list_del(&survey->list);
322 os_free(survey);
323 }
324}
325
326
Hai Shalomfdcde762020-04-02 11:19:20 -0700327static void acs_cleanup_mode(struct hostapd_hw_modes *mode)
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700328{
329 int i;
330 struct hostapd_channel_data *chan;
331
Hai Shalomfdcde762020-04-02 11:19:20 -0700332 for (i = 0; i < mode->num_channels; i++) {
333 chan = &mode->channels[i];
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700334
335 if (chan->flag & HOSTAPD_CHAN_SURVEY_LIST_INITIALIZED)
336 acs_clean_chan_surveys(chan);
337
338 dl_list_init(&chan->survey_list);
339 chan->flag |= HOSTAPD_CHAN_SURVEY_LIST_INITIALIZED;
340 chan->min_nf = 0;
Sunil Raviaf8751c2023-03-29 11:35:17 -0700341 chan->punct_bitmap = 0;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700342 }
Hai Shalomfdcde762020-04-02 11:19:20 -0700343}
344
345
346void acs_cleanup(struct hostapd_iface *iface)
347{
348 int i;
349
350 for (i = 0; i < iface->num_hw_features; i++)
351 acs_cleanup_mode(&iface->hw_features[i]);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700352
353 iface->chans_surveyed = 0;
354 iface->acs_num_completed_scans = 0;
355}
356
357
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800358static void acs_fail(struct hostapd_iface *iface)
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700359{
360 wpa_printf(MSG_ERROR, "ACS: Failed to start");
361 acs_cleanup(iface);
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -0800362 hostapd_disable_iface(iface);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700363}
364
365
366static long double
367acs_survey_interference_factor(struct freq_survey *survey, s8 min_nf)
368{
369 long double factor, busy, total;
370
371 if (survey->filled & SURVEY_HAS_CHAN_TIME_BUSY)
372 busy = survey->channel_time_busy;
373 else if (survey->filled & SURVEY_HAS_CHAN_TIME_RX)
374 busy = survey->channel_time_rx;
375 else {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700376 wpa_printf(MSG_ERROR, "ACS: Survey data missing");
377 return 0;
378 }
379
380 total = survey->channel_time;
381
382 if (survey->filled & SURVEY_HAS_CHAN_TIME_TX) {
383 busy -= survey->channel_time_tx;
384 total -= survey->channel_time_tx;
385 }
386
387 /* TODO: figure out the best multiplier for noise floor base */
388 factor = pow(10, survey->nf / 5.0L) +
Roshan Pius3a1667e2018-07-03 15:17:14 -0700389 (total ? (busy / total) : 0) *
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700390 pow(2, pow(10, (long double) survey->nf / 10.0L) -
391 pow(10, (long double) min_nf / 10.0L));
392
393 return factor;
394}
395
396
397static void
398acs_survey_chan_interference_factor(struct hostapd_iface *iface,
399 struct hostapd_channel_data *chan)
400{
401 struct freq_survey *survey;
402 unsigned int i = 0;
403 long double int_factor = 0;
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800404 unsigned count = 0;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700405
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700406 if (dl_list_empty(&chan->survey_list) ||
407 (chan->flag & HOSTAPD_CHAN_DISABLED))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700408 return;
409
410 chan->interference_factor = 0;
411
412 dl_list_for_each(survey, &chan->survey_list, struct freq_survey, list)
413 {
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800414 i++;
415
416 if (!acs_survey_is_sufficient(survey)) {
417 wpa_printf(MSG_DEBUG, "ACS: %d: insufficient data", i);
418 continue;
419 }
420
421 count++;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700422 int_factor = acs_survey_interference_factor(survey,
423 iface->lowest_nf);
424 chan->interference_factor += int_factor;
425 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 -0800426 i, chan->min_nf, int_factor,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700427 survey->nf, (unsigned long) survey->channel_time,
428 (unsigned long) survey->channel_time_busy,
429 (unsigned long) survey->channel_time_rx);
430 }
431
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700432 if (count)
433 chan->interference_factor /= count;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700434}
435
436
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000437static bool acs_usable_bw_chan(const struct hostapd_channel_data *chan,
438 enum bw_type bw)
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700439{
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000440 unsigned int i = 0;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700441
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000442 while (bw_desc[bw][i].first != -1) {
443 if (chan->freq == bw_desc[bw][i].first)
444 return true;
445 i++;
446 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700447
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000448 return false;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700449}
450
451
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000452static int acs_get_bw_center_chan(int freq, enum bw_type bw)
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800453{
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000454 unsigned int i = 0;
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800455
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000456 while (bw_desc[bw][i].first != -1) {
457 if (freq >= bw_desc[bw][i].first &&
458 freq <= bw_desc[bw][i].last)
459 return bw_desc[bw][i].center_chan;
460 i++;
461 }
Hai Shalom74f70d42019-02-11 14:42:39 -0800462
463 return 0;
464}
465
466
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700467static int acs_survey_is_sufficient(struct freq_survey *survey)
468{
469 if (!(survey->filled & SURVEY_HAS_NF)) {
Sunil Ravi89eba102022-09-13 21:04:37 -0700470 wpa_printf(MSG_INFO,
471 "ACS: Survey for freq %d is missing noise floor",
472 survey->freq);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700473 return 0;
474 }
475
476 if (!(survey->filled & SURVEY_HAS_CHAN_TIME)) {
Sunil Ravi89eba102022-09-13 21:04:37 -0700477 wpa_printf(MSG_INFO,
478 "ACS: Survey for freq %d is missing channel time",
479 survey->freq);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700480 return 0;
481 }
482
483 if (!(survey->filled & SURVEY_HAS_CHAN_TIME_BUSY) &&
484 !(survey->filled & SURVEY_HAS_CHAN_TIME_RX)) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800485 wpa_printf(MSG_INFO,
Sunil Ravi89eba102022-09-13 21:04:37 -0700486 "ACS: Survey for freq %d is missing RX and busy time (at least one is required)",
487 survey->freq);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700488 return 0;
489 }
490
491 return 1;
492}
493
494
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700495static int acs_survey_list_is_sufficient(struct hostapd_channel_data *chan)
496{
497 struct freq_survey *survey;
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800498 int ret = -1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700499
500 dl_list_for_each(survey, &chan->survey_list, struct freq_survey, list)
501 {
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800502 if (acs_survey_is_sufficient(survey)) {
503 ret = 1;
504 break;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700505 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800506 ret = 0;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700507 }
508
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800509 if (ret == -1)
510 ret = 1; /* no survey list entries */
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700511
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800512 if (!ret) {
513 wpa_printf(MSG_INFO,
514 "ACS: Channel %d has insufficient survey data",
515 chan->chan);
516 }
517
518 return ret;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700519}
520
521
Hai Shalomfdcde762020-04-02 11:19:20 -0700522static int acs_surveys_are_sufficient_mode(struct hostapd_hw_modes *mode)
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700523{
524 int i;
525 struct hostapd_channel_data *chan;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700526
Hai Shalomfdcde762020-04-02 11:19:20 -0700527 for (i = 0; i < mode->num_channels; i++) {
528 chan = &mode->channels[i];
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700529 if (!(chan->flag & HOSTAPD_CHAN_DISABLED) &&
530 acs_survey_list_is_sufficient(chan))
Hai Shalomfdcde762020-04-02 11:19:20 -0700531 return 1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700532 }
533
Hai Shalomfdcde762020-04-02 11:19:20 -0700534 return 0;
535}
536
537
538static int acs_surveys_are_sufficient(struct hostapd_iface *iface)
539{
540 int i;
541 struct hostapd_hw_modes *mode;
542
543 for (i = 0; i < iface->num_hw_features; i++) {
544 mode = &iface->hw_features[i];
545 if (!hostapd_hw_skip_mode(iface, mode) &&
546 acs_surveys_are_sufficient_mode(mode))
547 return 1;
548 }
549
550 return 0;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700551}
552
553
554static int acs_usable_chan(struct hostapd_channel_data *chan)
555{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700556 return !dl_list_empty(&chan->survey_list) &&
557 !(chan->flag & HOSTAPD_CHAN_DISABLED) &&
558 acs_survey_list_is_sufficient(chan);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700559}
560
561
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800562static int is_in_chanlist(struct hostapd_iface *iface,
563 struct hostapd_channel_data *chan)
564{
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700565 if (!iface->conf->acs_ch_list.num)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800566 return 1;
567
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700568 return freq_range_list_includes(&iface->conf->acs_ch_list, chan->chan);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800569}
570
571
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700572static int is_in_freqlist(struct hostapd_iface *iface,
573 struct hostapd_channel_data *chan)
574{
575 if (!iface->conf->acs_freq_list.num)
576 return 1;
577
578 return freq_range_list_includes(&iface->conf->acs_freq_list,
579 chan->freq);
580}
581
582
Hai Shalomfdcde762020-04-02 11:19:20 -0700583static void acs_survey_mode_interference_factor(
584 struct hostapd_iface *iface, struct hostapd_hw_modes *mode)
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700585{
586 int i;
587 struct hostapd_channel_data *chan;
588
Hai Shalomfdcde762020-04-02 11:19:20 -0700589 for (i = 0; i < mode->num_channels; i++) {
590 chan = &mode->channels[i];
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700591
592 if (!acs_usable_chan(chan))
593 continue;
594
Sunil Ravia04bd252022-05-02 22:54:18 -0700595 if ((chan->flag & HOSTAPD_CHAN_RADAR) &&
596 iface->conf->acs_exclude_dfs)
597 continue;
598
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800599 if (!is_in_chanlist(iface, chan))
600 continue;
601
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700602 if (!is_in_freqlist(iface, chan))
603 continue;
604
Hai Shaloma20dcd72022-02-04 13:43:00 -0800605 if (chan->max_tx_power < iface->conf->min_tx_power)
606 continue;
607
Sunil Ravi89eba102022-09-13 21:04:37 -0700608 if ((chan->flag & HOSTAPD_CHAN_INDOOR_ONLY) &&
609 iface->conf->country[2] == 0x4f)
610 continue;
611
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700612 wpa_printf(MSG_DEBUG, "ACS: Survey analysis for channel %d (%d MHz)",
613 chan->chan, chan->freq);
614
615 acs_survey_chan_interference_factor(iface, chan);
616
617 wpa_printf(MSG_DEBUG, "ACS: * interference factor average: %Lg",
618 chan->interference_factor);
619 }
620}
621
622
Hai Shalomfdcde762020-04-02 11:19:20 -0700623static void acs_survey_all_chans_interference_factor(
624 struct hostapd_iface *iface)
625{
626 int i;
627 struct hostapd_hw_modes *mode;
628
629 for (i = 0; i < iface->num_hw_features; i++) {
630 mode = &iface->hw_features[i];
631 if (!hostapd_hw_skip_mode(iface, mode))
632 acs_survey_mode_interference_factor(iface, mode);
633 }
634}
635
636
637static struct hostapd_channel_data *
638acs_find_chan_mode(struct hostapd_hw_modes *mode, int freq)
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700639{
640 struct hostapd_channel_data *chan;
641 int i;
642
Hai Shalomfdcde762020-04-02 11:19:20 -0700643 for (i = 0; i < mode->num_channels; i++) {
644 chan = &mode->channels[i];
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700645
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700646 if (chan->flag & HOSTAPD_CHAN_DISABLED)
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700647 continue;
648
649 if (chan->freq == freq)
650 return chan;
651 }
652
653 return NULL;
654}
655
656
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000657static enum hostapd_hw_mode
658acs_find_mode(struct hostapd_iface *iface, int freq)
659{
660 int i;
661 struct hostapd_hw_modes *mode;
662 struct hostapd_channel_data *chan;
663
664 for (i = 0; i < iface->num_hw_features; i++) {
665 mode = &iface->hw_features[i];
666 if (!hostapd_hw_skip_mode(iface, mode)) {
667 chan = acs_find_chan_mode(mode, freq);
668 if (chan)
669 return mode->mode;
670 }
671 }
672
673 return HOSTAPD_MODE_IEEE80211ANY;
674}
675
676
Hai Shalomfdcde762020-04-02 11:19:20 -0700677static struct hostapd_channel_data *
678acs_find_chan(struct hostapd_iface *iface, int freq)
679{
680 int i;
681 struct hostapd_hw_modes *mode;
682 struct hostapd_channel_data *chan;
683
684 for (i = 0; i < iface->num_hw_features; i++) {
685 mode = &iface->hw_features[i];
686 if (!hostapd_hw_skip_mode(iface, mode)) {
687 chan = acs_find_chan_mode(mode, freq);
688 if (chan)
689 return chan;
690 }
691 }
692
693 return NULL;
694}
695
696
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800697static int is_24ghz_mode(enum hostapd_hw_mode mode)
698{
699 return mode == HOSTAPD_MODE_IEEE80211B ||
700 mode == HOSTAPD_MODE_IEEE80211G;
701}
702
703
704static int is_common_24ghz_chan(int chan)
705{
706 return chan == 1 || chan == 6 || chan == 11;
707}
708
709
710#ifndef ACS_ADJ_WEIGHT
711#define ACS_ADJ_WEIGHT 0.85
712#endif /* ACS_ADJ_WEIGHT */
713
714#ifndef ACS_NEXT_ADJ_WEIGHT
715#define ACS_NEXT_ADJ_WEIGHT 0.55
716#endif /* ACS_NEXT_ADJ_WEIGHT */
717
718#ifndef ACS_24GHZ_PREFER_1_6_11
719/*
720 * Select commonly used channels 1, 6, 11 by default even if a neighboring
721 * channel has a smaller interference factor as long as it is not better by more
722 * than this multiplier.
723 */
724#define ACS_24GHZ_PREFER_1_6_11 0.8
725#endif /* ACS_24GHZ_PREFER_1_6_11 */
726
Sunil Raviaf8751c2023-03-29 11:35:17 -0700727
728#ifdef CONFIG_IEEE80211BE
729static void acs_update_puncturing_bitmap(struct hostapd_iface *iface,
730 struct hostapd_hw_modes *mode, u32 bw,
731 int n_chans,
732 struct hostapd_channel_data *chan,
733 long double factor,
734 int index_primary)
735{
736 struct hostapd_config *conf = iface->conf;
737 struct hostapd_channel_data *adj_chan = NULL, *first_chan = chan;
738 int i;
739 long double threshold;
740
741 /*
742 * If threshold is 0 or user configured puncturing pattern is
743 * available then don't add additional puncturing.
744 */
745 if (!conf->punct_acs_threshold || conf->punct_bitmap)
746 return;
747
748 if (is_24ghz_mode(mode->mode) || bw < 80)
749 return;
750
751 threshold = factor * conf->punct_acs_threshold / 100;
752 for (i = 0; i < n_chans; i++) {
753 int adj_freq;
754
755 if (i == index_primary)
756 continue; /* Cannot puncture primary channel */
757
758 if (i > index_primary)
759 adj_freq = chan->freq + (i - index_primary) * 20;
760 else
761 adj_freq = chan->freq - (index_primary - i) * 20;
762
763 adj_chan = acs_find_chan(iface, adj_freq);
764 if (!adj_chan) {
765 chan->punct_bitmap = 0;
766 return;
767 }
768
769 if (i == 0)
770 first_chan = adj_chan;
771
772 if (adj_chan->interference_factor > threshold)
773 chan->punct_bitmap |= BIT(i);
774 }
775
776 if (!is_punct_bitmap_valid(bw, (chan->freq - first_chan->freq) / 20,
777 chan->punct_bitmap))
778 chan->punct_bitmap = 0;
779}
780#endif /* CONFIG_IEEE80211BE */
781
782
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000783static bool
784acs_usable_bw320_chan(struct hostapd_iface *iface,
785 struct hostapd_channel_data *chan, int *bw320_offset)
786{
787 const char *bw320_str[] = { "320 MHz", "320 MHz-1", "320 MHz-2" };
788 int conf_bw320_offset = hostapd_get_bw320_offset(iface->conf);
789
790 *bw320_offset = 0;
791 switch (conf_bw320_offset) {
792 case 1:
793 if (acs_usable_bw_chan(chan, ACS_BW320_1))
794 *bw320_offset = 1;
795 break;
796 case 2:
797 if (acs_usable_bw_chan(chan, ACS_BW320_2))
798 *bw320_offset = 2;
799 break;
800 case 0:
801 default:
802 conf_bw320_offset = 0;
803 if (acs_usable_bw_chan(chan, ACS_BW320_1))
804 *bw320_offset = 1;
805 else if (acs_usable_bw_chan(chan, ACS_BW320_2))
806 *bw320_offset = 2;
807 break;
808 }
809
810 if (!*bw320_offset)
811 wpa_printf(MSG_DEBUG,
812 "ACS: Channel %d: not allowed as primary channel for %s bandwidth",
813 chan->chan, bw320_str[conf_bw320_offset]);
814
815 return *bw320_offset != 0;
816}
817
818
Hai Shalomfdcde762020-04-02 11:19:20 -0700819static void
820acs_find_ideal_chan_mode(struct hostapd_iface *iface,
821 struct hostapd_hw_modes *mode,
822 int n_chans, u32 bw,
823 struct hostapd_channel_data **rand_chan,
824 struct hostapd_channel_data **ideal_chan,
825 long double *ideal_factor)
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700826{
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000827 struct hostapd_channel_data *chan, *adj_chan = NULL, *best;
Hai Shalomfdcde762020-04-02 11:19:20 -0700828 long double factor;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700829 int i, j;
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000830 int bw320_offset = 0, ideal_bw320_offset = 0;
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800831 unsigned int k;
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000832 int secondary_channel = 1, freq_offset;
833
834 if (is_24ghz_mode(mode->mode))
835 secondary_channel = iface->conf->secondary_channel;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700836
Hai Shalomfdcde762020-04-02 11:19:20 -0700837 for (i = 0; i < mode->num_channels; i++) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000838 double total_weight = 0;
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800839 struct acs_bias *bias, tmp_bias;
840
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000841 chan = &mode->channels[i];
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700842
Hai Shalom74f70d42019-02-11 14:42:39 -0800843 /* Since in the current ACS implementation the first channel is
844 * always a primary channel, skip channels not available as
845 * primary until more sophisticated channel selection is
Sunil Raviaf8751c2023-03-29 11:35:17 -0700846 * implemented.
847 *
848 * If this implementation is changed to allow any channel in
849 * the bandwidth to be the primary one, the last parameter to
850 * acs_update_puncturing_bitmap() should be changed to the index
851 * of the primary channel
852 */
Hai Shalom74f70d42019-02-11 14:42:39 -0800853 if (!chan_pri_allowed(chan))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700854 continue;
855
Sunil Ravia04bd252022-05-02 22:54:18 -0700856 if ((chan->flag & HOSTAPD_CHAN_RADAR) &&
857 iface->conf->acs_exclude_dfs)
858 continue;
859
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800860 if (!is_in_chanlist(iface, chan))
861 continue;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700862
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700863 if (!is_in_freqlist(iface, chan))
864 continue;
865
Hai Shaloma20dcd72022-02-04 13:43:00 -0800866 if (chan->max_tx_power < iface->conf->min_tx_power)
867 continue;
868
Sunil Ravi89eba102022-09-13 21:04:37 -0700869 if ((chan->flag & HOSTAPD_CHAN_INDOOR_ONLY) &&
870 iface->conf->country[2] == 0x4f)
871 continue;
872
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000873 if (!chan_bw_allowed(chan, bw, secondary_channel != -1, 1)) {
Hai Shalom74f70d42019-02-11 14:42:39 -0800874 wpa_printf(MSG_DEBUG,
875 "ACS: Channel %d: BW %u is not supported",
876 chan->chan, bw);
877 continue;
878 }
879
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700880 /* HT40 on 5 GHz has a limited set of primary channels as per
881 * 11n Annex J */
Hai Shalomfdcde762020-04-02 11:19:20 -0700882 if (mode->mode == HOSTAPD_MODE_IEEE80211A &&
Hai Shaloma20dcd72022-02-04 13:43:00 -0800883 ((iface->conf->ieee80211n &&
884 iface->conf->secondary_channel) ||
885 is_6ghz_freq(chan->freq)) &&
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000886 !acs_usable_bw_chan(chan, ACS_BW40)) {
Hai Shaloma20dcd72022-02-04 13:43:00 -0800887 wpa_printf(MSG_DEBUG,
888 "ACS: Channel %d: not allowed as primary channel for 40 MHz bandwidth",
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700889 chan->chan);
890 continue;
891 }
892
Hai Shalomfdcde762020-04-02 11:19:20 -0700893 if (mode->mode == HOSTAPD_MODE_IEEE80211A &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000894 (iface->conf->ieee80211ac || iface->conf->ieee80211ax ||
895 iface->conf->ieee80211be)) {
Hai Shalom81f62d82019-07-22 12:10:00 -0700896 if (hostapd_get_oper_chwidth(iface->conf) ==
Sunil8cd6f4d2022-06-28 18:40:46 +0000897 CONF_OPER_CHWIDTH_80MHZ &&
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000898 !acs_usable_bw_chan(chan, ACS_BW80)) {
Hai Shalom74f70d42019-02-11 14:42:39 -0800899 wpa_printf(MSG_DEBUG,
Hai Shaloma20dcd72022-02-04 13:43:00 -0800900 "ACS: Channel %d: not allowed as primary channel for 80 MHz bandwidth",
Hai Shalom74f70d42019-02-11 14:42:39 -0800901 chan->chan);
902 continue;
903 }
904
Hai Shalom81f62d82019-07-22 12:10:00 -0700905 if (hostapd_get_oper_chwidth(iface->conf) ==
Sunil8cd6f4d2022-06-28 18:40:46 +0000906 CONF_OPER_CHWIDTH_160MHZ &&
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000907 !acs_usable_bw_chan(chan, ACS_BW160)) {
Hai Shalom74f70d42019-02-11 14:42:39 -0800908 wpa_printf(MSG_DEBUG,
Hai Shaloma20dcd72022-02-04 13:43:00 -0800909 "ACS: Channel %d: not allowed as primary channel for 160 MHz bandwidth",
Hai Shalom74f70d42019-02-11 14:42:39 -0800910 chan->chan);
911 continue;
912 }
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800913 }
914
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000915 if (mode->mode == HOSTAPD_MODE_IEEE80211A &&
916 iface->conf->ieee80211be) {
917 if (hostapd_get_oper_chwidth(iface->conf) ==
918 CONF_OPER_CHWIDTH_320MHZ &&
919 !acs_usable_bw320_chan(iface, chan, &bw320_offset))
920 continue;
921 }
922
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700923 factor = 0;
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000924 best = NULL;
925 if (acs_usable_chan(chan)) {
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700926 factor = chan->interference_factor;
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000927 total_weight = 1;
928 best = chan;
929 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700930
931 for (j = 1; j < n_chans; j++) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000932 adj_chan = acs_find_chan(iface, chan->freq +
933 j * secondary_channel * 20);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700934 if (!adj_chan)
935 break;
936
Hai Shalom74f70d42019-02-11 14:42:39 -0800937 if (!chan_bw_allowed(adj_chan, bw, 1, 0)) {
938 wpa_printf(MSG_DEBUG,
939 "ACS: PRI Channel %d: secondary channel %d BW %u is not supported",
940 chan->chan, adj_chan->chan, bw);
941 break;
942 }
943
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000944 if (!acs_usable_chan(adj_chan))
945 continue;
946
947 factor += adj_chan->interference_factor;
948 total_weight += 1;
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000949
950 /* find the best channel in this segment */
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000951 if (!best || adj_chan->interference_factor <
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000952 best->interference_factor)
953 best = adj_chan;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700954 }
955
956 if (j != n_chans) {
957 wpa_printf(MSG_DEBUG, "ACS: Channel %d: not enough bandwidth",
958 chan->chan);
959 continue;
960 }
961
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000962 /* If the AP is in the 5 GHz or 6 GHz band, lets prefer a less
963 * crowded primary channel if one was found in the segment */
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000964 if (iface->current_mode &&
965 iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A &&
966 best && chan != best) {
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000967 wpa_printf(MSG_DEBUG,
968 "ACS: promoting channel %d over %d (less interference %Lg/%Lg)",
969 best->chan, chan->chan,
970 chan->interference_factor,
971 best->interference_factor);
972 chan = best;
973 }
974
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700975 /* 2.4 GHz has overlapping 20 MHz channels. Include adjacent
976 * channel interference factor. */
Hai Shalomfdcde762020-04-02 11:19:20 -0700977 if (is_24ghz_mode(mode->mode)) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700978 for (j = 0; j < n_chans; j++) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000979 freq_offset = j * 20 * secondary_channel;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700980 adj_chan = acs_find_chan(iface, chan->freq +
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000981 freq_offset - 5);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800982 if (adj_chan && acs_usable_chan(adj_chan)) {
983 factor += ACS_ADJ_WEIGHT *
984 adj_chan->interference_factor;
985 total_weight += ACS_ADJ_WEIGHT;
986 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700987
988 adj_chan = acs_find_chan(iface, chan->freq +
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000989 freq_offset - 10);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800990 if (adj_chan && acs_usable_chan(adj_chan)) {
991 factor += ACS_NEXT_ADJ_WEIGHT *
992 adj_chan->interference_factor;
993 total_weight += ACS_NEXT_ADJ_WEIGHT;
994 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700995
996 adj_chan = acs_find_chan(iface, chan->freq +
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000997 freq_offset + 5);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800998 if (adj_chan && acs_usable_chan(adj_chan)) {
999 factor += ACS_ADJ_WEIGHT *
1000 adj_chan->interference_factor;
1001 total_weight += ACS_ADJ_WEIGHT;
1002 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001003
1004 adj_chan = acs_find_chan(iface, chan->freq +
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001005 freq_offset + 10);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001006 if (adj_chan && acs_usable_chan(adj_chan)) {
1007 factor += ACS_NEXT_ADJ_WEIGHT *
1008 adj_chan->interference_factor;
1009 total_weight += ACS_NEXT_ADJ_WEIGHT;
1010 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001011 }
1012 }
1013
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001014 if (total_weight == 0)
1015 continue;
1016
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001017 factor /= total_weight;
1018
1019 bias = NULL;
1020 if (iface->conf->acs_chan_bias) {
1021 for (k = 0; k < iface->conf->num_acs_chan_bias; k++) {
1022 bias = &iface->conf->acs_chan_bias[k];
1023 if (bias->channel == chan->chan)
1024 break;
1025 bias = NULL;
1026 }
Hai Shalomfdcde762020-04-02 11:19:20 -07001027 } else if (is_24ghz_mode(mode->mode) &&
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001028 is_common_24ghz_chan(chan->chan)) {
1029 tmp_bias.channel = chan->chan;
1030 tmp_bias.bias = ACS_24GHZ_PREFER_1_6_11;
1031 bias = &tmp_bias;
1032 }
1033
1034 if (bias) {
1035 factor *= bias->bias;
1036 wpa_printf(MSG_DEBUG,
1037 "ACS: * channel %d: total interference = %Lg (%f bias)",
1038 chan->chan, factor, bias->bias);
1039 } else {
1040 wpa_printf(MSG_DEBUG,
1041 "ACS: * channel %d: total interference = %Lg",
1042 chan->chan, factor);
1043 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001044
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07001045 if (acs_usable_chan(chan) &&
Hai Shalomfdcde762020-04-02 11:19:20 -07001046 (!*ideal_chan || factor < *ideal_factor)) {
Sunil Raviaf8751c2023-03-29 11:35:17 -07001047 /* Reset puncturing bitmap for the previous ideal
1048 * channel */
1049 if (*ideal_chan)
1050 (*ideal_chan)->punct_bitmap = 0;
1051
Hai Shalomfdcde762020-04-02 11:19:20 -07001052 *ideal_factor = factor;
1053 *ideal_chan = chan;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001054 ideal_bw320_offset = bw320_offset;
Sunil Raviaf8751c2023-03-29 11:35:17 -07001055
1056#ifdef CONFIG_IEEE80211BE
1057 if (iface->conf->ieee80211be)
1058 acs_update_puncturing_bitmap(iface, mode, bw,
1059 n_chans, chan,
1060 factor, 0);
1061#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001062 }
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07001063
1064 /* This channel would at least be usable */
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001065 if (!(*rand_chan)) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001066 *rand_chan = chan;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001067 ideal_bw320_offset = bw320_offset;
1068 }
Hai Shalomfdcde762020-04-02 11:19:20 -07001069 }
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001070
1071 hostapd_set_and_check_bw320_offset(iface->conf, ideal_bw320_offset);
Hai Shalomfdcde762020-04-02 11:19:20 -07001072}
1073
1074
1075/*
1076 * At this point it's assumed chan->interference_factor has been computed.
1077 * This function should be reusable regardless of interference computation
1078 * option (survey, BSS, spectral, ...). chan->interference factor must be
1079 * summable (i.e., must be always greater than zero).
1080 */
1081static struct hostapd_channel_data *
1082acs_find_ideal_chan(struct hostapd_iface *iface)
1083{
1084 struct hostapd_channel_data *ideal_chan = NULL,
1085 *rand_chan = NULL;
1086 long double ideal_factor = 0;
1087 int i;
1088 int n_chans = 1;
1089 u32 bw;
1090 struct hostapd_hw_modes *mode;
1091
Hai Shaloma20dcd72022-02-04 13:43:00 -08001092 if (is_6ghz_op_class(iface->conf->op_class)) {
1093 bw = op_class_to_bandwidth(iface->conf->op_class);
1094 n_chans = bw / 20;
1095 goto bw_selected;
1096 }
1097
Hai Shalomfdcde762020-04-02 11:19:20 -07001098 if (iface->conf->ieee80211n &&
1099 iface->conf->secondary_channel)
1100 n_chans = 2;
1101
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001102 if (iface->conf->ieee80211ac || iface->conf->ieee80211ax ||
1103 iface->conf->ieee80211be) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001104 switch (hostapd_get_oper_chwidth(iface->conf)) {
Sunil8cd6f4d2022-06-28 18:40:46 +00001105 case CONF_OPER_CHWIDTH_80MHZ:
Hai Shalomfdcde762020-04-02 11:19:20 -07001106 n_chans = 4;
1107 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00001108 case CONF_OPER_CHWIDTH_160MHZ:
Hai Shalomfdcde762020-04-02 11:19:20 -07001109 n_chans = 8;
1110 break;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001111 case CONF_OPER_CHWIDTH_320MHZ:
1112 n_chans = 16;
1113 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00001114 default:
1115 break;
Hai Shalomfdcde762020-04-02 11:19:20 -07001116 }
1117 }
1118
1119 bw = num_chan_to_bw(n_chans);
1120
Hai Shaloma20dcd72022-02-04 13:43:00 -08001121bw_selected:
Hai Shalomfdcde762020-04-02 11:19:20 -07001122 /* TODO: VHT/HE80+80. Update acs_adjust_center_freq() too. */
1123
1124 wpa_printf(MSG_DEBUG,
1125 "ACS: Survey analysis for selected bandwidth %d MHz", bw);
1126
1127 for (i = 0; i < iface->num_hw_features; i++) {
1128 mode = &iface->hw_features[i];
1129 if (!hostapd_hw_skip_mode(iface, mode))
1130 acs_find_ideal_chan_mode(iface, mode, n_chans, bw,
1131 &rand_chan, &ideal_chan,
1132 &ideal_factor);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001133 }
1134
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07001135 if (ideal_chan) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001136 wpa_printf(MSG_DEBUG, "ACS: Ideal channel is %d (%d MHz) with total interference factor of %Lg",
1137 ideal_chan->chan, ideal_chan->freq, ideal_factor);
Sunil Raviaf8751c2023-03-29 11:35:17 -07001138
1139#ifdef CONFIG_IEEE80211BE
1140 if (iface->conf->punct_acs_threshold)
1141 wpa_printf(MSG_DEBUG, "ACS: RU puncturing bitmap 0x%x",
1142 ideal_chan->punct_bitmap);
1143#endif /* CONFIG_IEEE80211BE */
1144
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07001145 return ideal_chan;
1146 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001147
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07001148 return rand_chan;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001149}
1150
1151
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001152static void acs_adjust_secondary(struct hostapd_iface *iface)
1153{
1154 unsigned int i;
1155
1156 /* When working with bandwidth over 20 MHz on the 5 GHz or 6 GHz band,
1157 * ACS can return a secondary channel which is not the first channel of
1158 * the segment and we need to adjust. */
1159 if (!iface->conf->secondary_channel ||
1160 acs_find_mode(iface, iface->freq) != HOSTAPD_MODE_IEEE80211A)
1161 return;
1162
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001163 wpa_printf(MSG_DEBUG,
1164 "ACS: Adjusting HT/VHT/HE/EHT secondary frequency");
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001165
1166 for (i = 0; bw_desc[ACS_BW40][i].first != -1; i++) {
1167 if (iface->freq == bw_desc[ACS_BW40][i].first)
1168 iface->conf->secondary_channel = 1;
1169 else if (iface->freq == bw_desc[ACS_BW40][i].last)
1170 iface->conf->secondary_channel = -1;
1171 }
1172}
1173
1174
Hai Shalom81f62d82019-07-22 12:10:00 -07001175static void acs_adjust_center_freq(struct hostapd_iface *iface)
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001176{
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001177 int center;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001178
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001179 wpa_printf(MSG_DEBUG, "ACS: Adjusting center frequency");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001180
Hai Shalom81f62d82019-07-22 12:10:00 -07001181 switch (hostapd_get_oper_chwidth(iface->conf)) {
Sunil8cd6f4d2022-06-28 18:40:46 +00001182 case CONF_OPER_CHWIDTH_USE_HT:
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001183 if (iface->conf->secondary_channel &&
1184 iface->freq >= 2400 && iface->freq < 2500)
1185 center = iface->conf->channel +
1186 2 * iface->conf->secondary_channel;
1187 else if (iface->conf->secondary_channel)
1188 center = acs_get_bw_center_chan(iface->freq, ACS_BW40);
1189 else
1190 center = iface->conf->channel;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001191 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00001192 case CONF_OPER_CHWIDTH_80MHZ:
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001193 center = acs_get_bw_center_chan(iface->freq, ACS_BW80);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001194 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00001195 case CONF_OPER_CHWIDTH_160MHZ:
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001196 center = acs_get_bw_center_chan(iface->freq, ACS_BW160);
Hai Shalom74f70d42019-02-11 14:42:39 -08001197 break;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001198 case CONF_OPER_CHWIDTH_320MHZ:
1199 switch (hostapd_get_bw320_offset(iface->conf)) {
1200 case 1:
1201 center = acs_get_bw_center_chan(iface->freq,
1202 ACS_BW320_1);
1203 break;
1204 case 2:
1205 center = acs_get_bw_center_chan(iface->freq,
1206 ACS_BW320_2);
1207 break;
1208 default:
1209 wpa_printf(MSG_INFO,
1210 "ACS: BW320 offset is not selected");
1211 return;
1212 }
1213
1214 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001215 default:
1216 /* TODO: How can this be calculated? Adjust
1217 * acs_find_ideal_chan() */
Hai Shalom74f70d42019-02-11 14:42:39 -08001218 wpa_printf(MSG_INFO,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001219 "ACS: Only VHT20/40/80/160/320 is supported now");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001220 return;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001221 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001222
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001223 hostapd_set_oper_centr_freq_seg0_idx(iface->conf, center);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001224}
1225
1226
1227static int acs_study_survey_based(struct hostapd_iface *iface)
1228{
1229 wpa_printf(MSG_DEBUG, "ACS: Trying survey-based ACS");
1230
1231 if (!iface->chans_surveyed) {
1232 wpa_printf(MSG_ERROR, "ACS: Unable to collect survey data");
1233 return -1;
1234 }
1235
1236 if (!acs_surveys_are_sufficient(iface)) {
1237 wpa_printf(MSG_ERROR, "ACS: Surveys have insufficient data");
1238 return -1;
1239 }
1240
Hai Shalomfdcde762020-04-02 11:19:20 -07001241 acs_survey_all_chans_interference_factor(iface);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001242 return 0;
1243}
1244
1245
1246static int acs_study_options(struct hostapd_iface *iface)
1247{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001248 if (acs_study_survey_based(iface) == 0)
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001249 return 0;
1250
1251 /* TODO: If no surveys are available/sufficient this is a good
1252 * place to fallback to BSS-based ACS */
1253
1254 return -1;
1255}
1256
1257
1258static void acs_study(struct hostapd_iface *iface)
1259{
1260 struct hostapd_channel_data *ideal_chan;
1261 int err;
1262
1263 err = acs_study_options(iface);
1264 if (err < 0) {
1265 wpa_printf(MSG_ERROR, "ACS: All study options have failed");
1266 goto fail;
1267 }
1268
1269 ideal_chan = acs_find_ideal_chan(iface);
1270 if (!ideal_chan) {
1271 wpa_printf(MSG_ERROR, "ACS: Failed to compute ideal channel");
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07001272 err = -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001273 goto fail;
1274 }
1275
1276 iface->conf->channel = ideal_chan->chan;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001277 iface->freq = ideal_chan->freq;
Sunil Raviaf8751c2023-03-29 11:35:17 -07001278#ifdef CONFIG_IEEE80211BE
1279 iface->conf->punct_bitmap = ideal_chan->punct_bitmap;
1280#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001281
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001282 if (iface->conf->ieee80211ac || iface->conf->ieee80211ax ||
1283 iface->conf->ieee80211be) {
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001284 acs_adjust_secondary(iface);
Hai Shalom81f62d82019-07-22 12:10:00 -07001285 acs_adjust_center_freq(iface);
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001286 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001287
Sunil Ravi3e88b0e2022-08-10 22:56:55 +00001288 err = hostapd_select_hw_mode(iface);
1289 if (err) {
Sunil Ravi89eba102022-09-13 21:04:37 -07001290 wpa_printf(MSG_ERROR,
1291 "ACS: Could not (err: %d) select hw_mode for freq=%d channel=%d",
Sunil Ravi3e88b0e2022-08-10 22:56:55 +00001292 err, iface->freq, iface->conf->channel);
1293 err = -1;
1294 goto fail;
1295 }
1296
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07001297 err = 0;
1298fail:
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001299 /*
1300 * hostapd_setup_interface_complete() will return -1 on failure,
1301 * 0 on success and 0 is HOSTAPD_CHAN_VALID :)
1302 */
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07001303 if (hostapd_acs_completed(iface, err) == HOSTAPD_CHAN_VALID) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001304 acs_cleanup(iface);
1305 return;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001306 }
1307
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07001308 /* This can possibly happen if channel parameters (secondary
1309 * channel, center frequencies) are misconfigured */
1310 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 -07001311 acs_fail(iface);
1312}
1313
1314
1315static void acs_scan_complete(struct hostapd_iface *iface)
1316{
1317 int err;
1318
1319 iface->scan_cb = NULL;
1320
1321 wpa_printf(MSG_DEBUG, "ACS: Using survey based algorithm (acs_num_scans=%d)",
1322 iface->conf->acs_num_scans);
1323
1324 err = hostapd_drv_get_survey(iface->bss[0], 0);
1325 if (err) {
1326 wpa_printf(MSG_ERROR, "ACS: Failed to get survey data");
Dmitry Shmidt15907092014-03-25 10:42:57 -07001327 goto fail;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001328 }
1329
1330 if (++iface->acs_num_completed_scans < iface->conf->acs_num_scans) {
1331 err = acs_request_scan(iface);
1332 if (err) {
1333 wpa_printf(MSG_ERROR, "ACS: Failed to request scan");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001334 goto fail;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001335 }
1336
1337 return;
1338 }
1339
1340 acs_study(iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001341 return;
1342fail:
1343 hostapd_acs_completed(iface, 1);
1344 acs_fail(iface);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001345}
1346
1347
Hai Shalomfdcde762020-04-02 11:19:20 -07001348static int * acs_request_scan_add_freqs(struct hostapd_iface *iface,
1349 struct hostapd_hw_modes *mode,
1350 int *freq)
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001351{
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001352 struct hostapd_channel_data *chan;
Hai Shalomfdcde762020-04-02 11:19:20 -07001353 int i;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001354
Hai Shalomfdcde762020-04-02 11:19:20 -07001355 for (i = 0; i < mode->num_channels; i++) {
1356 chan = &mode->channels[i];
Sunil Ravia04bd252022-05-02 22:54:18 -07001357 if ((chan->flag & HOSTAPD_CHAN_DISABLED) ||
1358 ((chan->flag & HOSTAPD_CHAN_RADAR) &&
1359 iface->conf->acs_exclude_dfs))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001360 continue;
1361
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001362 if (!is_in_chanlist(iface, chan))
1363 continue;
1364
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001365 if (!is_in_freqlist(iface, chan))
1366 continue;
1367
Hai Shaloma20dcd72022-02-04 13:43:00 -08001368 if (chan->max_tx_power < iface->conf->min_tx_power)
1369 continue;
1370
Sunil Ravi89eba102022-09-13 21:04:37 -07001371 if ((chan->flag & HOSTAPD_CHAN_INDOOR_ONLY) &&
1372 iface->conf->country[2] == 0x4f)
1373 continue;
1374
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001375 *freq++ = chan->freq;
1376 }
Hai Shalomfdcde762020-04-02 11:19:20 -07001377
1378 return freq;
1379}
1380
1381
1382static int acs_request_scan(struct hostapd_iface *iface)
1383{
1384 struct wpa_driver_scan_params params;
1385 int i, *freq;
1386 int num_channels;
1387 struct hostapd_hw_modes *mode;
1388
1389 os_memset(&params, 0, sizeof(params));
1390
1391 num_channels = 0;
1392 for (i = 0; i < iface->num_hw_features; i++) {
1393 mode = &iface->hw_features[i];
1394 if (!hostapd_hw_skip_mode(iface, mode))
1395 num_channels += mode->num_channels;
1396 }
1397
1398 params.freqs = os_calloc(num_channels + 1, sizeof(params.freqs[0]));
1399 if (params.freqs == NULL)
1400 return -1;
1401
1402 freq = params.freqs;
1403
1404 for (i = 0; i < iface->num_hw_features; i++) {
1405 mode = &iface->hw_features[i];
1406 if (!hostapd_hw_skip_mode(iface, mode))
1407 freq = acs_request_scan_add_freqs(iface, mode, freq);
1408 }
1409
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001410 *freq = 0;
1411
Neo Joue2881702019-10-17 11:32:28 +08001412 if (params.freqs == freq) {
1413 wpa_printf(MSG_ERROR, "ACS: No available channels found");
1414 os_free(params.freqs);
1415 return -1;
1416 }
1417
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001418 iface->scan_cb = acs_scan_complete;
1419
1420 wpa_printf(MSG_DEBUG, "ACS: Scanning %d / %d",
1421 iface->acs_num_completed_scans + 1,
1422 iface->conf->acs_num_scans);
1423
1424 if (hostapd_driver_scan(iface->bss[0], &params) < 0) {
1425 wpa_printf(MSG_ERROR, "ACS: Failed to request initial scan");
1426 acs_cleanup(iface);
Dmitry Shmidt15907092014-03-25 10:42:57 -07001427 os_free(params.freqs);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001428 return -1;
1429 }
1430
1431 os_free(params.freqs);
1432 return 0;
1433}
1434
1435
1436enum hostapd_chan_status acs_init(struct hostapd_iface *iface)
1437{
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001438 int err;
1439
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001440 wpa_printf(MSG_INFO, "ACS: Automatic channel selection started, this may take a bit");
1441
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001442 if (iface->drv_flags & WPA_DRIVER_FLAGS_ACS_OFFLOAD) {
1443 wpa_printf(MSG_INFO, "ACS: Offloading to driver");
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001444
1445 err = hostapd_drv_do_acs(iface->bss[0]);
1446 if (err) {
1447 if (err == 1)
1448 return HOSTAPD_CHAN_INVALID_NO_IR;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001449 return HOSTAPD_CHAN_INVALID;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001450 }
1451
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001452 return HOSTAPD_CHAN_ACS;
1453 }
1454
Hai Shalomfdcde762020-04-02 11:19:20 -07001455 if (!iface->current_mode &&
1456 iface->conf->hw_mode != HOSTAPD_MODE_IEEE80211ANY)
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001457 return HOSTAPD_CHAN_INVALID;
1458
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001459 acs_cleanup(iface);
1460
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001461 if (acs_request_scan(iface) < 0)
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001462 return HOSTAPD_CHAN_INVALID;
1463
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001464 hostapd_set_state(iface, HAPD_IFACE_ACS);
1465 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, ACS_EVENT_STARTED);
1466
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001467 return HOSTAPD_CHAN_ACS;
1468}