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