1e5b75505Sopenharmony_ci/* 2e5b75505Sopenharmony_ci * ACS - Automatic Channel Selection module 3e5b75505Sopenharmony_ci * Copyright (c) 2011, Atheros Communications 4e5b75505Sopenharmony_ci * Copyright (c) 2013, Qualcomm Atheros, Inc. 5e5b75505Sopenharmony_ci * 6e5b75505Sopenharmony_ci * This software may be distributed under the terms of the BSD license. 7e5b75505Sopenharmony_ci * See README for more details. 8e5b75505Sopenharmony_ci */ 9e5b75505Sopenharmony_ci 10e5b75505Sopenharmony_ci#include "utils/includes.h" 11e5b75505Sopenharmony_ci#include <math.h> 12e5b75505Sopenharmony_ci 13e5b75505Sopenharmony_ci#include "utils/common.h" 14e5b75505Sopenharmony_ci#include "utils/list.h" 15e5b75505Sopenharmony_ci#include "common/ieee802_11_defs.h" 16e5b75505Sopenharmony_ci#include "common/hw_features_common.h" 17e5b75505Sopenharmony_ci#include "common/wpa_ctrl.h" 18e5b75505Sopenharmony_ci#include "drivers/driver.h" 19e5b75505Sopenharmony_ci#include "hostapd.h" 20e5b75505Sopenharmony_ci#include "ap_drv_ops.h" 21e5b75505Sopenharmony_ci#include "ap_config.h" 22e5b75505Sopenharmony_ci#include "hw_features.h" 23e5b75505Sopenharmony_ci#include "acs.h" 24e5b75505Sopenharmony_ci 25e5b75505Sopenharmony_ci/* 26e5b75505Sopenharmony_ci * Automatic Channel Selection 27e5b75505Sopenharmony_ci * =========================== 28e5b75505Sopenharmony_ci * 29e5b75505Sopenharmony_ci * More info at 30e5b75505Sopenharmony_ci * ------------ 31e5b75505Sopenharmony_ci * http://wireless.kernel.org/en/users/Documentation/acs 32e5b75505Sopenharmony_ci * 33e5b75505Sopenharmony_ci * How to use 34e5b75505Sopenharmony_ci * ---------- 35e5b75505Sopenharmony_ci * - make sure you have CONFIG_ACS=y in hostapd's .config 36e5b75505Sopenharmony_ci * - use channel=0 or channel=acs to enable ACS 37e5b75505Sopenharmony_ci * 38e5b75505Sopenharmony_ci * How does it work 39e5b75505Sopenharmony_ci * ---------------- 40e5b75505Sopenharmony_ci * 1. passive scans are used to collect survey data 41e5b75505Sopenharmony_ci * (it is assumed that scan trigger collection of survey data in driver) 42e5b75505Sopenharmony_ci * 2. interference factor is calculated for each channel 43e5b75505Sopenharmony_ci * 3. ideal channel is picked depending on channel width by using adjacent 44e5b75505Sopenharmony_ci * channel interference factors 45e5b75505Sopenharmony_ci * 46e5b75505Sopenharmony_ci * Known limitations 47e5b75505Sopenharmony_ci * ----------------- 48e5b75505Sopenharmony_ci * - Current implementation depends heavily on the amount of time willing to 49e5b75505Sopenharmony_ci * spend gathering survey data during hostapd startup. Short traffic bursts 50e5b75505Sopenharmony_ci * may be missed and a suboptimal channel may be picked. 51e5b75505Sopenharmony_ci * - Ideal channel may end up overlapping a channel with 40 MHz intolerant BSS 52e5b75505Sopenharmony_ci * 53e5b75505Sopenharmony_ci * Todo / Ideas 54e5b75505Sopenharmony_ci * ------------ 55e5b75505Sopenharmony_ci * - implement other interference computation methods 56e5b75505Sopenharmony_ci * - BSS/RSSI based 57e5b75505Sopenharmony_ci * - spectral scan based 58e5b75505Sopenharmony_ci * (should be possibly to hook this up with current ACS scans) 59e5b75505Sopenharmony_ci * - add wpa_supplicant support (for P2P) 60e5b75505Sopenharmony_ci * - collect a histogram of interference over time allowing more educated 61e5b75505Sopenharmony_ci * guess about an ideal channel (perhaps CSA could be used to migrate AP to a 62e5b75505Sopenharmony_ci * new "better" channel while running) 63e5b75505Sopenharmony_ci * - include neighboring BSS scan to avoid conflicts with 40 MHz intolerant BSSs 64e5b75505Sopenharmony_ci * when choosing the ideal channel 65e5b75505Sopenharmony_ci * 66e5b75505Sopenharmony_ci * Survey interference factor implementation details 67e5b75505Sopenharmony_ci * ------------------------------------------------- 68e5b75505Sopenharmony_ci * Generic interference_factor in struct hostapd_channel_data is used. 69e5b75505Sopenharmony_ci * 70e5b75505Sopenharmony_ci * The survey interference factor is defined as the ratio of the 71e5b75505Sopenharmony_ci * observed busy time over the time we spent on the channel, 72e5b75505Sopenharmony_ci * this value is then amplified by the observed noise floor on 73e5b75505Sopenharmony_ci * the channel in comparison to the lowest noise floor observed 74e5b75505Sopenharmony_ci * on the entire band. 75e5b75505Sopenharmony_ci * 76e5b75505Sopenharmony_ci * This corresponds to: 77e5b75505Sopenharmony_ci * --- 78e5b75505Sopenharmony_ci * (busy time - tx time) / (active time - tx time) * 2^(chan_nf + band_min_nf) 79e5b75505Sopenharmony_ci * --- 80e5b75505Sopenharmony_ci * 81e5b75505Sopenharmony_ci * The coefficient of 2 reflects the way power in "far-field" 82e5b75505Sopenharmony_ci * radiation decreases as the square of distance from the antenna [1]. 83e5b75505Sopenharmony_ci * What this does is it decreases the observed busy time ratio if the 84e5b75505Sopenharmony_ci * noise observed was low but increases it if the noise was high, 85e5b75505Sopenharmony_ci * proportionally to the way "far field" radiation changes over 86e5b75505Sopenharmony_ci * distance. 87e5b75505Sopenharmony_ci * 88e5b75505Sopenharmony_ci * If channel busy time is not available the fallback is to use channel RX time. 89e5b75505Sopenharmony_ci * 90e5b75505Sopenharmony_ci * Since noise floor is in dBm it is necessary to convert it into Watts so that 91e5b75505Sopenharmony_ci * combined channel interference (e.g., HT40, which uses two channels) can be 92e5b75505Sopenharmony_ci * calculated easily. 93e5b75505Sopenharmony_ci * --- 94e5b75505Sopenharmony_ci * (busy time - tx time) / (active time - tx time) * 95e5b75505Sopenharmony_ci * 2^(10^(chan_nf/10) + 10^(band_min_nf/10)) 96e5b75505Sopenharmony_ci * --- 97e5b75505Sopenharmony_ci * 98e5b75505Sopenharmony_ci * However to account for cases where busy/rx time is 0 (channel load is then 99e5b75505Sopenharmony_ci * 0%) channel noise floor signal power is combined into the equation so a 100e5b75505Sopenharmony_ci * channel with lower noise floor is preferred. The equation becomes: 101e5b75505Sopenharmony_ci * --- 102e5b75505Sopenharmony_ci * 10^(chan_nf/5) + (busy time - tx time) / (active time - tx time) * 103e5b75505Sopenharmony_ci * 2^(10^(chan_nf/10) + 10^(band_min_nf/10)) 104e5b75505Sopenharmony_ci * --- 105e5b75505Sopenharmony_ci * 106e5b75505Sopenharmony_ci * All this "interference factor" is purely subjective and only time 107e5b75505Sopenharmony_ci * will tell how usable this is. By using the minimum noise floor we 108e5b75505Sopenharmony_ci * remove any possible issues due to card calibration. The computation 109e5b75505Sopenharmony_ci * of the interference factor then is dependent on what the card itself 110e5b75505Sopenharmony_ci * picks up as the minimum noise, not an actual real possible card 111e5b75505Sopenharmony_ci * noise value. 112e5b75505Sopenharmony_ci * 113e5b75505Sopenharmony_ci * Total interference computation details 114e5b75505Sopenharmony_ci * -------------------------------------- 115e5b75505Sopenharmony_ci * The above channel interference factor is calculated with no respect to 116e5b75505Sopenharmony_ci * target operational bandwidth. 117e5b75505Sopenharmony_ci * 118e5b75505Sopenharmony_ci * To find an ideal channel the above data is combined by taking into account 119e5b75505Sopenharmony_ci * the target operational bandwidth and selected band. E.g., on 2.4 GHz channels 120e5b75505Sopenharmony_ci * overlap with 20 MHz bandwidth, but there is no overlap for 20 MHz bandwidth 121e5b75505Sopenharmony_ci * on 5 GHz. 122e5b75505Sopenharmony_ci * 123e5b75505Sopenharmony_ci * Each valid and possible channel spec (i.e., channel + width) is taken and its 124e5b75505Sopenharmony_ci * interference factor is computed by summing up interferences of each channel 125e5b75505Sopenharmony_ci * it overlaps. The one with least total interference is picked up. 126e5b75505Sopenharmony_ci * 127e5b75505Sopenharmony_ci * Note: This implies base channel interference factor must be non-negative 128e5b75505Sopenharmony_ci * allowing easy summing up. 129e5b75505Sopenharmony_ci * 130e5b75505Sopenharmony_ci * Example ACS analysis printout 131e5b75505Sopenharmony_ci * ----------------------------- 132e5b75505Sopenharmony_ci * 133e5b75505Sopenharmony_ci * ACS: Trying survey-based ACS 134e5b75505Sopenharmony_ci * ACS: Survey analysis for channel 1 (2412 MHz) 135e5b75505Sopenharmony_ci * ACS: 1: min_nf=-113 interference_factor=0.0802469 nf=-113 time=162 busy=0 rx=13 136e5b75505Sopenharmony_ci * ACS: 2: min_nf=-113 interference_factor=0.0745342 nf=-113 time=161 busy=0 rx=12 137e5b75505Sopenharmony_ci * ACS: 3: min_nf=-113 interference_factor=0.0679012 nf=-113 time=162 busy=0 rx=11 138e5b75505Sopenharmony_ci * ACS: 4: min_nf=-113 interference_factor=0.0310559 nf=-113 time=161 busy=0 rx=5 139e5b75505Sopenharmony_ci * ACS: 5: min_nf=-113 interference_factor=0.0248447 nf=-113 time=161 busy=0 rx=4 140e5b75505Sopenharmony_ci * ACS: * interference factor average: 0.0557166 141e5b75505Sopenharmony_ci * ACS: Survey analysis for channel 2 (2417 MHz) 142e5b75505Sopenharmony_ci * ACS: 1: min_nf=-113 interference_factor=0.0185185 nf=-113 time=162 busy=0 rx=3 143e5b75505Sopenharmony_ci * ACS: 2: min_nf=-113 interference_factor=0.0246914 nf=-113 time=162 busy=0 rx=4 144e5b75505Sopenharmony_ci * ACS: 3: min_nf=-113 interference_factor=0.037037 nf=-113 time=162 busy=0 rx=6 145e5b75505Sopenharmony_ci * ACS: 4: min_nf=-113 interference_factor=0.149068 nf=-113 time=161 busy=0 rx=24 146e5b75505Sopenharmony_ci * ACS: 5: min_nf=-113 interference_factor=0.0248447 nf=-113 time=161 busy=0 rx=4 147e5b75505Sopenharmony_ci * ACS: * interference factor average: 0.050832 148e5b75505Sopenharmony_ci * ACS: Survey analysis for channel 3 (2422 MHz) 149e5b75505Sopenharmony_ci * ACS: 1: min_nf=-113 interference_factor=2.51189e-23 nf=-113 time=162 busy=0 rx=0 150e5b75505Sopenharmony_ci * ACS: 2: min_nf=-113 interference_factor=0.0185185 nf=-113 time=162 busy=0 rx=3 151e5b75505Sopenharmony_ci * ACS: 3: min_nf=-113 interference_factor=0.0186335 nf=-113 time=161 busy=0 rx=3 152e5b75505Sopenharmony_ci * ACS: 4: min_nf=-113 interference_factor=0.0186335 nf=-113 time=161 busy=0 rx=3 153e5b75505Sopenharmony_ci * ACS: 5: min_nf=-113 interference_factor=0.0186335 nf=-113 time=161 busy=0 rx=3 154e5b75505Sopenharmony_ci * ACS: * interference factor average: 0.0148838 155e5b75505Sopenharmony_ci * ACS: Survey analysis for channel 4 (2427 MHz) 156e5b75505Sopenharmony_ci * ACS: 1: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0 157e5b75505Sopenharmony_ci * ACS: 2: min_nf=-114 interference_factor=0.0555556 nf=-114 time=162 busy=0 rx=9 158e5b75505Sopenharmony_ci * ACS: 3: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=161 busy=0 rx=0 159e5b75505Sopenharmony_ci * ACS: 4: min_nf=-114 interference_factor=0.0186335 nf=-114 time=161 busy=0 rx=3 160e5b75505Sopenharmony_ci * ACS: 5: min_nf=-114 interference_factor=0.00621118 nf=-114 time=161 busy=0 rx=1 161e5b75505Sopenharmony_ci * ACS: * interference factor average: 0.0160801 162e5b75505Sopenharmony_ci * ACS: Survey analysis for channel 5 (2432 MHz) 163e5b75505Sopenharmony_ci * ACS: 1: min_nf=-114 interference_factor=0.409938 nf=-113 time=161 busy=0 rx=66 164e5b75505Sopenharmony_ci * ACS: 2: min_nf=-114 interference_factor=0.0432099 nf=-113 time=162 busy=0 rx=7 165e5b75505Sopenharmony_ci * ACS: 3: min_nf=-114 interference_factor=0.0124224 nf=-113 time=161 busy=0 rx=2 166e5b75505Sopenharmony_ci * ACS: 4: min_nf=-114 interference_factor=0.677019 nf=-113 time=161 busy=0 rx=109 167e5b75505Sopenharmony_ci * ACS: 5: min_nf=-114 interference_factor=0.0186335 nf=-114 time=161 busy=0 rx=3 168e5b75505Sopenharmony_ci * ACS: * interference factor average: 0.232244 169e5b75505Sopenharmony_ci * ACS: Survey analysis for channel 6 (2437 MHz) 170e5b75505Sopenharmony_ci * ACS: 1: min_nf=-113 interference_factor=0.552795 nf=-113 time=161 busy=0 rx=89 171e5b75505Sopenharmony_ci * ACS: 2: min_nf=-113 interference_factor=0.0807453 nf=-112 time=161 busy=0 rx=13 172e5b75505Sopenharmony_ci * ACS: 3: min_nf=-113 interference_factor=0.0310559 nf=-113 time=161 busy=0 rx=5 173e5b75505Sopenharmony_ci * ACS: 4: min_nf=-113 interference_factor=0.434783 nf=-112 time=161 busy=0 rx=70 174e5b75505Sopenharmony_ci * ACS: 5: min_nf=-113 interference_factor=0.0621118 nf=-113 time=161 busy=0 rx=10 175e5b75505Sopenharmony_ci * ACS: * interference factor average: 0.232298 176e5b75505Sopenharmony_ci * ACS: Survey analysis for channel 7 (2442 MHz) 177e5b75505Sopenharmony_ci * ACS: 1: min_nf=-113 interference_factor=0.440994 nf=-112 time=161 busy=0 rx=71 178e5b75505Sopenharmony_ci * ACS: 2: min_nf=-113 interference_factor=0.385093 nf=-113 time=161 busy=0 rx=62 179e5b75505Sopenharmony_ci * ACS: 3: min_nf=-113 interference_factor=0.0372671 nf=-113 time=161 busy=0 rx=6 180e5b75505Sopenharmony_ci * ACS: 4: min_nf=-113 interference_factor=0.0372671 nf=-113 time=161 busy=0 rx=6 181e5b75505Sopenharmony_ci * ACS: 5: min_nf=-113 interference_factor=0.0745342 nf=-113 time=161 busy=0 rx=12 182e5b75505Sopenharmony_ci * ACS: * interference factor average: 0.195031 183e5b75505Sopenharmony_ci * ACS: Survey analysis for channel 8 (2447 MHz) 184e5b75505Sopenharmony_ci * ACS: 1: min_nf=-114 interference_factor=0.0496894 nf=-112 time=161 busy=0 rx=8 185e5b75505Sopenharmony_ci * ACS: 2: min_nf=-114 interference_factor=0.0496894 nf=-114 time=161 busy=0 rx=8 186e5b75505Sopenharmony_ci * ACS: 3: min_nf=-114 interference_factor=0.0372671 nf=-113 time=161 busy=0 rx=6 187e5b75505Sopenharmony_ci * ACS: 4: min_nf=-114 interference_factor=0.12963 nf=-113 time=162 busy=0 rx=21 188e5b75505Sopenharmony_ci * ACS: 5: min_nf=-114 interference_factor=0.166667 nf=-114 time=162 busy=0 rx=27 189e5b75505Sopenharmony_ci * ACS: * interference factor average: 0.0865885 190e5b75505Sopenharmony_ci * ACS: Survey analysis for channel 9 (2452 MHz) 191e5b75505Sopenharmony_ci * ACS: 1: min_nf=-114 interference_factor=0.0124224 nf=-114 time=161 busy=0 rx=2 192e5b75505Sopenharmony_ci * ACS: 2: min_nf=-114 interference_factor=0.0310559 nf=-114 time=161 busy=0 rx=5 193e5b75505Sopenharmony_ci * ACS: 3: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=161 busy=0 rx=0 194e5b75505Sopenharmony_ci * ACS: 4: min_nf=-114 interference_factor=0.00617284 nf=-114 time=162 busy=0 rx=1 195e5b75505Sopenharmony_ci * ACS: 5: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0 196e5b75505Sopenharmony_ci * ACS: * interference factor average: 0.00993022 197e5b75505Sopenharmony_ci * ACS: Survey analysis for channel 10 (2457 MHz) 198e5b75505Sopenharmony_ci * ACS: 1: min_nf=-114 interference_factor=0.00621118 nf=-114 time=161 busy=0 rx=1 199e5b75505Sopenharmony_ci * ACS: 2: min_nf=-114 interference_factor=0.00621118 nf=-114 time=161 busy=0 rx=1 200e5b75505Sopenharmony_ci * ACS: 3: min_nf=-114 interference_factor=0.00621118 nf=-114 time=161 busy=0 rx=1 201e5b75505Sopenharmony_ci * ACS: 4: min_nf=-114 interference_factor=0.0493827 nf=-114 time=162 busy=0 rx=8 202e5b75505Sopenharmony_ci * ACS: 5: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0 203e5b75505Sopenharmony_ci * ACS: * interference factor average: 0.0136033 204e5b75505Sopenharmony_ci * ACS: Survey analysis for channel 11 (2462 MHz) 205e5b75505Sopenharmony_ci * ACS: 1: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=161 busy=0 rx=0 206e5b75505Sopenharmony_ci * ACS: 2: min_nf=-114 interference_factor=2.51189e-23 nf=-113 time=161 busy=0 rx=0 207e5b75505Sopenharmony_ci * ACS: 3: min_nf=-114 interference_factor=2.51189e-23 nf=-113 time=161 busy=0 rx=0 208e5b75505Sopenharmony_ci * ACS: 4: min_nf=-114 interference_factor=0.0432099 nf=-114 time=162 busy=0 rx=7 209e5b75505Sopenharmony_ci * ACS: 5: min_nf=-114 interference_factor=0.0925926 nf=-114 time=162 busy=0 rx=15 210e5b75505Sopenharmony_ci * ACS: * interference factor average: 0.0271605 211e5b75505Sopenharmony_ci * ACS: Survey analysis for channel 12 (2467 MHz) 212e5b75505Sopenharmony_ci * ACS: 1: min_nf=-114 interference_factor=0.0621118 nf=-113 time=161 busy=0 rx=10 213e5b75505Sopenharmony_ci * ACS: 2: min_nf=-114 interference_factor=0.00621118 nf=-114 time=161 busy=0 rx=1 214e5b75505Sopenharmony_ci * ACS: 3: min_nf=-114 interference_factor=2.51189e-23 nf=-113 time=162 busy=0 rx=0 215e5b75505Sopenharmony_ci * ACS: 4: min_nf=-114 interference_factor=2.51189e-23 nf=-113 time=162 busy=0 rx=0 216e5b75505Sopenharmony_ci * ACS: 5: min_nf=-114 interference_factor=0.00617284 nf=-113 time=162 busy=0 rx=1 217e5b75505Sopenharmony_ci * ACS: * interference factor average: 0.0148992 218e5b75505Sopenharmony_ci * ACS: Survey analysis for channel 13 (2472 MHz) 219e5b75505Sopenharmony_ci * ACS: 1: min_nf=-114 interference_factor=0.0745342 nf=-114 time=161 busy=0 rx=12 220e5b75505Sopenharmony_ci * ACS: 2: min_nf=-114 interference_factor=0.0555556 nf=-114 time=162 busy=0 rx=9 221e5b75505Sopenharmony_ci * ACS: 3: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0 222e5b75505Sopenharmony_ci * ACS: 4: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0 223e5b75505Sopenharmony_ci * ACS: 5: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0 224e5b75505Sopenharmony_ci * ACS: * interference factor average: 0.0260179 225e5b75505Sopenharmony_ci * ACS: Survey analysis for selected bandwidth 20MHz 226e5b75505Sopenharmony_ci * ACS: * channel 1: total interference = 0.121432 227e5b75505Sopenharmony_ci * ACS: * channel 2: total interference = 0.137512 228e5b75505Sopenharmony_ci * ACS: * channel 3: total interference = 0.369757 229e5b75505Sopenharmony_ci * ACS: * channel 4: total interference = 0.546338 230e5b75505Sopenharmony_ci * ACS: * channel 5: total interference = 0.690538 231e5b75505Sopenharmony_ci * ACS: * channel 6: total interference = 0.762242 232e5b75505Sopenharmony_ci * ACS: * channel 7: total interference = 0.756092 233e5b75505Sopenharmony_ci * ACS: * channel 8: total interference = 0.537451 234e5b75505Sopenharmony_ci * ACS: * channel 9: total interference = 0.332313 235e5b75505Sopenharmony_ci * ACS: * channel 10: total interference = 0.152182 236e5b75505Sopenharmony_ci * ACS: * channel 11: total interference = 0.0916111 237e5b75505Sopenharmony_ci * ACS: * channel 12: total interference = 0.0816809 238e5b75505Sopenharmony_ci * ACS: * channel 13: total interference = 0.0680776 239e5b75505Sopenharmony_ci * ACS: Ideal channel is 13 (2472 MHz) with total interference factor of 0.0680776 240e5b75505Sopenharmony_ci * 241e5b75505Sopenharmony_ci * [1] http://en.wikipedia.org/wiki/Near_and_far_field 242e5b75505Sopenharmony_ci */ 243e5b75505Sopenharmony_ci 244e5b75505Sopenharmony_ci 245e5b75505Sopenharmony_cistatic int acs_request_scan(struct hostapd_iface *iface); 246e5b75505Sopenharmony_cistatic int acs_survey_is_sufficient(struct freq_survey *survey); 247e5b75505Sopenharmony_ci 248e5b75505Sopenharmony_ci 249e5b75505Sopenharmony_cistatic void acs_clean_chan_surveys(struct hostapd_channel_data *chan) 250e5b75505Sopenharmony_ci{ 251e5b75505Sopenharmony_ci struct freq_survey *survey, *tmp; 252e5b75505Sopenharmony_ci 253e5b75505Sopenharmony_ci if (dl_list_empty(&chan->survey_list)) 254e5b75505Sopenharmony_ci return; 255e5b75505Sopenharmony_ci 256e5b75505Sopenharmony_ci dl_list_for_each_safe(survey, tmp, &chan->survey_list, 257e5b75505Sopenharmony_ci struct freq_survey, list) { 258e5b75505Sopenharmony_ci dl_list_del(&survey->list); 259e5b75505Sopenharmony_ci os_free(survey); 260e5b75505Sopenharmony_ci } 261e5b75505Sopenharmony_ci} 262e5b75505Sopenharmony_ci 263e5b75505Sopenharmony_ci 264e5b75505Sopenharmony_civoid acs_cleanup(struct hostapd_iface *iface) 265e5b75505Sopenharmony_ci{ 266e5b75505Sopenharmony_ci int i; 267e5b75505Sopenharmony_ci struct hostapd_channel_data *chan; 268e5b75505Sopenharmony_ci 269e5b75505Sopenharmony_ci for (i = 0; i < iface->current_mode->num_channels; i++) { 270e5b75505Sopenharmony_ci chan = &iface->current_mode->channels[i]; 271e5b75505Sopenharmony_ci 272e5b75505Sopenharmony_ci if (chan->flag & HOSTAPD_CHAN_SURVEY_LIST_INITIALIZED) 273e5b75505Sopenharmony_ci acs_clean_chan_surveys(chan); 274e5b75505Sopenharmony_ci 275e5b75505Sopenharmony_ci dl_list_init(&chan->survey_list); 276e5b75505Sopenharmony_ci chan->flag |= HOSTAPD_CHAN_SURVEY_LIST_INITIALIZED; 277e5b75505Sopenharmony_ci chan->min_nf = 0; 278e5b75505Sopenharmony_ci } 279e5b75505Sopenharmony_ci 280e5b75505Sopenharmony_ci iface->chans_surveyed = 0; 281e5b75505Sopenharmony_ci iface->acs_num_completed_scans = 0; 282e5b75505Sopenharmony_ci} 283e5b75505Sopenharmony_ci 284e5b75505Sopenharmony_ci 285e5b75505Sopenharmony_cistatic void acs_fail(struct hostapd_iface *iface) 286e5b75505Sopenharmony_ci{ 287e5b75505Sopenharmony_ci wpa_printf(MSG_ERROR, "ACS: Failed to start"); 288e5b75505Sopenharmony_ci acs_cleanup(iface); 289e5b75505Sopenharmony_ci hostapd_disable_iface(iface); 290e5b75505Sopenharmony_ci} 291e5b75505Sopenharmony_ci 292e5b75505Sopenharmony_ci 293e5b75505Sopenharmony_cistatic long double 294e5b75505Sopenharmony_ciacs_survey_interference_factor(struct freq_survey *survey, s8 min_nf) 295e5b75505Sopenharmony_ci{ 296e5b75505Sopenharmony_ci long double factor, busy, total; 297e5b75505Sopenharmony_ci 298e5b75505Sopenharmony_ci if (survey->filled & SURVEY_HAS_CHAN_TIME_BUSY) 299e5b75505Sopenharmony_ci busy = survey->channel_time_busy; 300e5b75505Sopenharmony_ci else if (survey->filled & SURVEY_HAS_CHAN_TIME_RX) 301e5b75505Sopenharmony_ci busy = survey->channel_time_rx; 302e5b75505Sopenharmony_ci else { 303e5b75505Sopenharmony_ci /* This shouldn't really happen as survey data is checked in 304e5b75505Sopenharmony_ci * acs_sanity_check() */ 305e5b75505Sopenharmony_ci wpa_printf(MSG_ERROR, "ACS: Survey data missing"); 306e5b75505Sopenharmony_ci return 0; 307e5b75505Sopenharmony_ci } 308e5b75505Sopenharmony_ci 309e5b75505Sopenharmony_ci total = survey->channel_time; 310e5b75505Sopenharmony_ci 311e5b75505Sopenharmony_ci if (survey->filled & SURVEY_HAS_CHAN_TIME_TX) { 312e5b75505Sopenharmony_ci busy -= survey->channel_time_tx; 313e5b75505Sopenharmony_ci total -= survey->channel_time_tx; 314e5b75505Sopenharmony_ci } 315e5b75505Sopenharmony_ci 316e5b75505Sopenharmony_ci /* TODO: figure out the best multiplier for noise floor base */ 317e5b75505Sopenharmony_ci factor = pow(10, survey->nf / 5.0L) + 318e5b75505Sopenharmony_ci (total ? (busy / total) : 0) * 319e5b75505Sopenharmony_ci pow(2, pow(10, (long double) survey->nf / 10.0L) - 320e5b75505Sopenharmony_ci pow(10, (long double) min_nf / 10.0L)); 321e5b75505Sopenharmony_ci 322e5b75505Sopenharmony_ci return factor; 323e5b75505Sopenharmony_ci} 324e5b75505Sopenharmony_ci 325e5b75505Sopenharmony_ci 326e5b75505Sopenharmony_cistatic void 327e5b75505Sopenharmony_ciacs_survey_chan_interference_factor(struct hostapd_iface *iface, 328e5b75505Sopenharmony_ci struct hostapd_channel_data *chan) 329e5b75505Sopenharmony_ci{ 330e5b75505Sopenharmony_ci struct freq_survey *survey; 331e5b75505Sopenharmony_ci unsigned int i = 0; 332e5b75505Sopenharmony_ci long double int_factor = 0; 333e5b75505Sopenharmony_ci unsigned count = 0; 334e5b75505Sopenharmony_ci 335e5b75505Sopenharmony_ci if (dl_list_empty(&chan->survey_list) || 336e5b75505Sopenharmony_ci (chan->flag & HOSTAPD_CHAN_DISABLED)) 337e5b75505Sopenharmony_ci return; 338e5b75505Sopenharmony_ci 339e5b75505Sopenharmony_ci chan->interference_factor = 0; 340e5b75505Sopenharmony_ci 341e5b75505Sopenharmony_ci dl_list_for_each(survey, &chan->survey_list, struct freq_survey, list) 342e5b75505Sopenharmony_ci { 343e5b75505Sopenharmony_ci i++; 344e5b75505Sopenharmony_ci 345e5b75505Sopenharmony_ci if (!acs_survey_is_sufficient(survey)) { 346e5b75505Sopenharmony_ci wpa_printf(MSG_DEBUG, "ACS: %d: insufficient data", i); 347e5b75505Sopenharmony_ci continue; 348e5b75505Sopenharmony_ci } 349e5b75505Sopenharmony_ci 350e5b75505Sopenharmony_ci count++; 351e5b75505Sopenharmony_ci int_factor = acs_survey_interference_factor(survey, 352e5b75505Sopenharmony_ci iface->lowest_nf); 353e5b75505Sopenharmony_ci chan->interference_factor += int_factor; 354e5b75505Sopenharmony_ci wpa_printf(MSG_DEBUG, "ACS: %d: min_nf=%d interference_factor=%Lg nf=%d time=%lu busy=%lu rx=%lu", 355e5b75505Sopenharmony_ci i, chan->min_nf, int_factor, 356e5b75505Sopenharmony_ci survey->nf, (unsigned long) survey->channel_time, 357e5b75505Sopenharmony_ci (unsigned long) survey->channel_time_busy, 358e5b75505Sopenharmony_ci (unsigned long) survey->channel_time_rx); 359e5b75505Sopenharmony_ci } 360e5b75505Sopenharmony_ci 361e5b75505Sopenharmony_ci if (count) 362e5b75505Sopenharmony_ci chan->interference_factor /= count; 363e5b75505Sopenharmony_ci} 364e5b75505Sopenharmony_ci 365e5b75505Sopenharmony_ci 366e5b75505Sopenharmony_cistatic int acs_usable_ht40_chan(const struct hostapd_channel_data *chan) 367e5b75505Sopenharmony_ci{ 368e5b75505Sopenharmony_ci const int allowed[] = { 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 369e5b75505Sopenharmony_ci 157, 184, 192 }; 370e5b75505Sopenharmony_ci unsigned int i; 371e5b75505Sopenharmony_ci 372e5b75505Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(allowed); i++) 373e5b75505Sopenharmony_ci if (chan->chan == allowed[i]) 374e5b75505Sopenharmony_ci return 1; 375e5b75505Sopenharmony_ci 376e5b75505Sopenharmony_ci return 0; 377e5b75505Sopenharmony_ci} 378e5b75505Sopenharmony_ci 379e5b75505Sopenharmony_ci 380e5b75505Sopenharmony_cistatic int acs_usable_vht80_chan(const struct hostapd_channel_data *chan) 381e5b75505Sopenharmony_ci{ 382e5b75505Sopenharmony_ci const int allowed[] = { 36, 52, 100, 116, 132, 149 }; 383e5b75505Sopenharmony_ci unsigned int i; 384e5b75505Sopenharmony_ci 385e5b75505Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(allowed); i++) 386e5b75505Sopenharmony_ci if (chan->chan == allowed[i]) 387e5b75505Sopenharmony_ci return 1; 388e5b75505Sopenharmony_ci 389e5b75505Sopenharmony_ci return 0; 390e5b75505Sopenharmony_ci} 391e5b75505Sopenharmony_ci 392e5b75505Sopenharmony_ci 393e5b75505Sopenharmony_cistatic int acs_usable_vht160_chan(const struct hostapd_channel_data *chan) 394e5b75505Sopenharmony_ci{ 395e5b75505Sopenharmony_ci const int allowed[] = { 36, 100 }; 396e5b75505Sopenharmony_ci unsigned int i; 397e5b75505Sopenharmony_ci 398e5b75505Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(allowed); i++) 399e5b75505Sopenharmony_ci if (chan->chan == allowed[i]) 400e5b75505Sopenharmony_ci return 1; 401e5b75505Sopenharmony_ci 402e5b75505Sopenharmony_ci return 0; 403e5b75505Sopenharmony_ci} 404e5b75505Sopenharmony_ci 405e5b75505Sopenharmony_ci 406e5b75505Sopenharmony_cistatic int acs_survey_is_sufficient(struct freq_survey *survey) 407e5b75505Sopenharmony_ci{ 408e5b75505Sopenharmony_ci if (!(survey->filled & SURVEY_HAS_NF)) { 409e5b75505Sopenharmony_ci wpa_printf(MSG_INFO, "ACS: Survey is missing noise floor"); 410e5b75505Sopenharmony_ci return 0; 411e5b75505Sopenharmony_ci } 412e5b75505Sopenharmony_ci 413e5b75505Sopenharmony_ci if (!(survey->filled & SURVEY_HAS_CHAN_TIME)) { 414e5b75505Sopenharmony_ci wpa_printf(MSG_INFO, "ACS: Survey is missing channel time"); 415e5b75505Sopenharmony_ci return 0; 416e5b75505Sopenharmony_ci } 417e5b75505Sopenharmony_ci 418e5b75505Sopenharmony_ci if (!(survey->filled & SURVEY_HAS_CHAN_TIME_BUSY) && 419e5b75505Sopenharmony_ci !(survey->filled & SURVEY_HAS_CHAN_TIME_RX)) { 420e5b75505Sopenharmony_ci wpa_printf(MSG_INFO, 421e5b75505Sopenharmony_ci "ACS: Survey is missing RX and busy time (at least one is required)"); 422e5b75505Sopenharmony_ci return 0; 423e5b75505Sopenharmony_ci } 424e5b75505Sopenharmony_ci 425e5b75505Sopenharmony_ci return 1; 426e5b75505Sopenharmony_ci} 427e5b75505Sopenharmony_ci 428e5b75505Sopenharmony_ci 429e5b75505Sopenharmony_cistatic int acs_survey_list_is_sufficient(struct hostapd_channel_data *chan) 430e5b75505Sopenharmony_ci{ 431e5b75505Sopenharmony_ci struct freq_survey *survey; 432e5b75505Sopenharmony_ci int ret = -1; 433e5b75505Sopenharmony_ci 434e5b75505Sopenharmony_ci dl_list_for_each(survey, &chan->survey_list, struct freq_survey, list) 435e5b75505Sopenharmony_ci { 436e5b75505Sopenharmony_ci if (acs_survey_is_sufficient(survey)) { 437e5b75505Sopenharmony_ci ret = 1; 438e5b75505Sopenharmony_ci break; 439e5b75505Sopenharmony_ci } 440e5b75505Sopenharmony_ci ret = 0; 441e5b75505Sopenharmony_ci } 442e5b75505Sopenharmony_ci 443e5b75505Sopenharmony_ci if (ret == -1) 444e5b75505Sopenharmony_ci ret = 1; /* no survey list entries */ 445e5b75505Sopenharmony_ci 446e5b75505Sopenharmony_ci if (!ret) { 447e5b75505Sopenharmony_ci wpa_printf(MSG_INFO, 448e5b75505Sopenharmony_ci "ACS: Channel %d has insufficient survey data", 449e5b75505Sopenharmony_ci chan->chan); 450e5b75505Sopenharmony_ci } 451e5b75505Sopenharmony_ci 452e5b75505Sopenharmony_ci return ret; 453e5b75505Sopenharmony_ci} 454e5b75505Sopenharmony_ci 455e5b75505Sopenharmony_ci 456e5b75505Sopenharmony_cistatic int acs_surveys_are_sufficient(struct hostapd_iface *iface) 457e5b75505Sopenharmony_ci{ 458e5b75505Sopenharmony_ci int i; 459e5b75505Sopenharmony_ci struct hostapd_channel_data *chan; 460e5b75505Sopenharmony_ci int valid = 0; 461e5b75505Sopenharmony_ci 462e5b75505Sopenharmony_ci for (i = 0; i < iface->current_mode->num_channels; i++) { 463e5b75505Sopenharmony_ci chan = &iface->current_mode->channels[i]; 464e5b75505Sopenharmony_ci if (!(chan->flag & HOSTAPD_CHAN_DISABLED) && 465e5b75505Sopenharmony_ci acs_survey_list_is_sufficient(chan)) 466e5b75505Sopenharmony_ci valid++; 467e5b75505Sopenharmony_ci } 468e5b75505Sopenharmony_ci 469e5b75505Sopenharmony_ci /* We need at least survey data for one channel */ 470e5b75505Sopenharmony_ci return !!valid; 471e5b75505Sopenharmony_ci} 472e5b75505Sopenharmony_ci 473e5b75505Sopenharmony_ci 474e5b75505Sopenharmony_cistatic int acs_usable_chan(struct hostapd_channel_data *chan) 475e5b75505Sopenharmony_ci{ 476e5b75505Sopenharmony_ci return !dl_list_empty(&chan->survey_list) && 477e5b75505Sopenharmony_ci !(chan->flag & HOSTAPD_CHAN_DISABLED) && 478e5b75505Sopenharmony_ci acs_survey_list_is_sufficient(chan); 479e5b75505Sopenharmony_ci} 480e5b75505Sopenharmony_ci 481e5b75505Sopenharmony_ci 482e5b75505Sopenharmony_cistatic int is_in_chanlist(struct hostapd_iface *iface, 483e5b75505Sopenharmony_ci struct hostapd_channel_data *chan) 484e5b75505Sopenharmony_ci{ 485e5b75505Sopenharmony_ci if (!iface->conf->acs_ch_list.num) 486e5b75505Sopenharmony_ci return 1; 487e5b75505Sopenharmony_ci 488e5b75505Sopenharmony_ci return freq_range_list_includes(&iface->conf->acs_ch_list, chan->chan); 489e5b75505Sopenharmony_ci} 490e5b75505Sopenharmony_ci 491e5b75505Sopenharmony_ci 492e5b75505Sopenharmony_cistatic void acs_survey_all_chans_intereference_factor( 493e5b75505Sopenharmony_ci struct hostapd_iface *iface) 494e5b75505Sopenharmony_ci{ 495e5b75505Sopenharmony_ci int i; 496e5b75505Sopenharmony_ci struct hostapd_channel_data *chan; 497e5b75505Sopenharmony_ci 498e5b75505Sopenharmony_ci for (i = 0; i < iface->current_mode->num_channels; i++) { 499e5b75505Sopenharmony_ci chan = &iface->current_mode->channels[i]; 500e5b75505Sopenharmony_ci 501e5b75505Sopenharmony_ci if (!acs_usable_chan(chan)) 502e5b75505Sopenharmony_ci continue; 503e5b75505Sopenharmony_ci 504e5b75505Sopenharmony_ci if (!is_in_chanlist(iface, chan)) 505e5b75505Sopenharmony_ci continue; 506e5b75505Sopenharmony_ci 507e5b75505Sopenharmony_ci wpa_printf(MSG_DEBUG, "ACS: Survey analysis for channel %d (%d MHz)", 508e5b75505Sopenharmony_ci chan->chan, chan->freq); 509e5b75505Sopenharmony_ci 510e5b75505Sopenharmony_ci acs_survey_chan_interference_factor(iface, chan); 511e5b75505Sopenharmony_ci 512e5b75505Sopenharmony_ci wpa_printf(MSG_DEBUG, "ACS: * interference factor average: %Lg", 513e5b75505Sopenharmony_ci chan->interference_factor); 514e5b75505Sopenharmony_ci } 515e5b75505Sopenharmony_ci} 516e5b75505Sopenharmony_ci 517e5b75505Sopenharmony_ci 518e5b75505Sopenharmony_cistatic struct hostapd_channel_data *acs_find_chan(struct hostapd_iface *iface, 519e5b75505Sopenharmony_ci int freq) 520e5b75505Sopenharmony_ci{ 521e5b75505Sopenharmony_ci struct hostapd_channel_data *chan; 522e5b75505Sopenharmony_ci int i; 523e5b75505Sopenharmony_ci 524e5b75505Sopenharmony_ci for (i = 0; i < iface->current_mode->num_channels; i++) { 525e5b75505Sopenharmony_ci chan = &iface->current_mode->channels[i]; 526e5b75505Sopenharmony_ci 527e5b75505Sopenharmony_ci if (chan->flag & HOSTAPD_CHAN_DISABLED) 528e5b75505Sopenharmony_ci continue; 529e5b75505Sopenharmony_ci 530e5b75505Sopenharmony_ci if (chan->freq == freq) 531e5b75505Sopenharmony_ci return chan; 532e5b75505Sopenharmony_ci } 533e5b75505Sopenharmony_ci 534e5b75505Sopenharmony_ci return NULL; 535e5b75505Sopenharmony_ci} 536e5b75505Sopenharmony_ci 537e5b75505Sopenharmony_ci 538e5b75505Sopenharmony_cistatic int is_24ghz_mode(enum hostapd_hw_mode mode) 539e5b75505Sopenharmony_ci{ 540e5b75505Sopenharmony_ci return mode == HOSTAPD_MODE_IEEE80211B || 541e5b75505Sopenharmony_ci mode == HOSTAPD_MODE_IEEE80211G; 542e5b75505Sopenharmony_ci} 543e5b75505Sopenharmony_ci 544e5b75505Sopenharmony_ci 545e5b75505Sopenharmony_cistatic int is_common_24ghz_chan(int chan) 546e5b75505Sopenharmony_ci{ 547e5b75505Sopenharmony_ci return chan == 1 || chan == 6 || chan == 11; 548e5b75505Sopenharmony_ci} 549e5b75505Sopenharmony_ci 550e5b75505Sopenharmony_ci 551e5b75505Sopenharmony_ci#ifndef ACS_ADJ_WEIGHT 552e5b75505Sopenharmony_ci#define ACS_ADJ_WEIGHT 0.85 553e5b75505Sopenharmony_ci#endif /* ACS_ADJ_WEIGHT */ 554e5b75505Sopenharmony_ci 555e5b75505Sopenharmony_ci#ifndef ACS_NEXT_ADJ_WEIGHT 556e5b75505Sopenharmony_ci#define ACS_NEXT_ADJ_WEIGHT 0.55 557e5b75505Sopenharmony_ci#endif /* ACS_NEXT_ADJ_WEIGHT */ 558e5b75505Sopenharmony_ci 559e5b75505Sopenharmony_ci#ifndef ACS_24GHZ_PREFER_1_6_11 560e5b75505Sopenharmony_ci/* 561e5b75505Sopenharmony_ci * Select commonly used channels 1, 6, 11 by default even if a neighboring 562e5b75505Sopenharmony_ci * channel has a smaller interference factor as long as it is not better by more 563e5b75505Sopenharmony_ci * than this multiplier. 564e5b75505Sopenharmony_ci */ 565e5b75505Sopenharmony_ci#define ACS_24GHZ_PREFER_1_6_11 0.8 566e5b75505Sopenharmony_ci#endif /* ACS_24GHZ_PREFER_1_6_11 */ 567e5b75505Sopenharmony_ci 568e5b75505Sopenharmony_ci/* 569e5b75505Sopenharmony_ci * At this point it's assumed chan->interface_factor has been computed. 570e5b75505Sopenharmony_ci * This function should be reusable regardless of interference computation 571e5b75505Sopenharmony_ci * option (survey, BSS, spectral, ...). chan->interference factor must be 572e5b75505Sopenharmony_ci * summable (i.e., must be always greater than zero). 573e5b75505Sopenharmony_ci */ 574e5b75505Sopenharmony_cistatic struct hostapd_channel_data * 575e5b75505Sopenharmony_ciacs_find_ideal_chan(struct hostapd_iface *iface) 576e5b75505Sopenharmony_ci{ 577e5b75505Sopenharmony_ci struct hostapd_channel_data *chan, *adj_chan, *ideal_chan = NULL, 578e5b75505Sopenharmony_ci *rand_chan = NULL; 579e5b75505Sopenharmony_ci long double factor, ideal_factor = 0; 580e5b75505Sopenharmony_ci int i, j; 581e5b75505Sopenharmony_ci int n_chans = 1; 582e5b75505Sopenharmony_ci u32 bw; 583e5b75505Sopenharmony_ci unsigned int k; 584e5b75505Sopenharmony_ci 585e5b75505Sopenharmony_ci /* TODO: HT40- support */ 586e5b75505Sopenharmony_ci 587e5b75505Sopenharmony_ci if (iface->conf->ieee80211n && 588e5b75505Sopenharmony_ci iface->conf->secondary_channel == -1) { 589e5b75505Sopenharmony_ci wpa_printf(MSG_ERROR, "ACS: HT40- is not supported yet. Please try HT40+"); 590e5b75505Sopenharmony_ci return NULL; 591e5b75505Sopenharmony_ci } 592e5b75505Sopenharmony_ci 593e5b75505Sopenharmony_ci if (iface->conf->ieee80211n && 594e5b75505Sopenharmony_ci iface->conf->secondary_channel) 595e5b75505Sopenharmony_ci n_chans = 2; 596e5b75505Sopenharmony_ci 597e5b75505Sopenharmony_ci if (iface->conf->ieee80211ac || iface->conf->ieee80211ax) { 598e5b75505Sopenharmony_ci switch (hostapd_get_oper_chwidth(iface->conf)) { 599e5b75505Sopenharmony_ci case CHANWIDTH_80MHZ: 600e5b75505Sopenharmony_ci n_chans = 4; 601e5b75505Sopenharmony_ci break; 602e5b75505Sopenharmony_ci case CHANWIDTH_160MHZ: 603e5b75505Sopenharmony_ci n_chans = 8; 604e5b75505Sopenharmony_ci break; 605e5b75505Sopenharmony_ci } 606e5b75505Sopenharmony_ci } 607e5b75505Sopenharmony_ci 608e5b75505Sopenharmony_ci bw = num_chan_to_bw(n_chans); 609e5b75505Sopenharmony_ci 610e5b75505Sopenharmony_ci /* TODO: VHT/HE80+80. Update acs_adjust_center_freq() too. */ 611e5b75505Sopenharmony_ci 612e5b75505Sopenharmony_ci wpa_printf(MSG_DEBUG, 613e5b75505Sopenharmony_ci "ACS: Survey analysis for selected bandwidth %d MHz", bw); 614e5b75505Sopenharmony_ci 615e5b75505Sopenharmony_ci for (i = 0; i < iface->current_mode->num_channels; i++) { 616e5b75505Sopenharmony_ci double total_weight; 617e5b75505Sopenharmony_ci struct acs_bias *bias, tmp_bias; 618e5b75505Sopenharmony_ci 619e5b75505Sopenharmony_ci chan = &iface->current_mode->channels[i]; 620e5b75505Sopenharmony_ci 621e5b75505Sopenharmony_ci /* Since in the current ACS implementation the first channel is 622e5b75505Sopenharmony_ci * always a primary channel, skip channels not available as 623e5b75505Sopenharmony_ci * primary until more sophisticated channel selection is 624e5b75505Sopenharmony_ci * implemented. */ 625e5b75505Sopenharmony_ci if (!chan_pri_allowed(chan)) 626e5b75505Sopenharmony_ci continue; 627e5b75505Sopenharmony_ci 628e5b75505Sopenharmony_ci if (!is_in_chanlist(iface, chan)) 629e5b75505Sopenharmony_ci continue; 630e5b75505Sopenharmony_ci 631e5b75505Sopenharmony_ci if (!chan_bw_allowed(chan, bw, 1, 1)) { 632e5b75505Sopenharmony_ci wpa_printf(MSG_DEBUG, 633e5b75505Sopenharmony_ci "ACS: Channel %d: BW %u is not supported", 634e5b75505Sopenharmony_ci chan->chan, bw); 635e5b75505Sopenharmony_ci continue; 636e5b75505Sopenharmony_ci } 637e5b75505Sopenharmony_ci 638e5b75505Sopenharmony_ci /* HT40 on 5 GHz has a limited set of primary channels as per 639e5b75505Sopenharmony_ci * 11n Annex J */ 640e5b75505Sopenharmony_ci if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A && 641e5b75505Sopenharmony_ci iface->conf->ieee80211n && 642e5b75505Sopenharmony_ci iface->conf->secondary_channel && 643e5b75505Sopenharmony_ci !acs_usable_ht40_chan(chan)) { 644e5b75505Sopenharmony_ci wpa_printf(MSG_DEBUG, "ACS: Channel %d: not allowed as primary channel for HT40", 645e5b75505Sopenharmony_ci chan->chan); 646e5b75505Sopenharmony_ci continue; 647e5b75505Sopenharmony_ci } 648e5b75505Sopenharmony_ci 649e5b75505Sopenharmony_ci if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A && 650e5b75505Sopenharmony_ci (iface->conf->ieee80211ac || iface->conf->ieee80211ax)) { 651e5b75505Sopenharmony_ci if (hostapd_get_oper_chwidth(iface->conf) == 652e5b75505Sopenharmony_ci CHANWIDTH_80MHZ && 653e5b75505Sopenharmony_ci !acs_usable_vht80_chan(chan)) { 654e5b75505Sopenharmony_ci wpa_printf(MSG_DEBUG, 655e5b75505Sopenharmony_ci "ACS: Channel %d: not allowed as primary channel for VHT80", 656e5b75505Sopenharmony_ci chan->chan); 657e5b75505Sopenharmony_ci continue; 658e5b75505Sopenharmony_ci } 659e5b75505Sopenharmony_ci 660e5b75505Sopenharmony_ci if (hostapd_get_oper_chwidth(iface->conf) == 661e5b75505Sopenharmony_ci CHANWIDTH_160MHZ && 662e5b75505Sopenharmony_ci !acs_usable_vht160_chan(chan)) { 663e5b75505Sopenharmony_ci wpa_printf(MSG_DEBUG, 664e5b75505Sopenharmony_ci "ACS: Channel %d: not allowed as primary channel for VHT160", 665e5b75505Sopenharmony_ci chan->chan); 666e5b75505Sopenharmony_ci continue; 667e5b75505Sopenharmony_ci } 668e5b75505Sopenharmony_ci } 669e5b75505Sopenharmony_ci 670e5b75505Sopenharmony_ci factor = 0; 671e5b75505Sopenharmony_ci if (acs_usable_chan(chan)) 672e5b75505Sopenharmony_ci factor = chan->interference_factor; 673e5b75505Sopenharmony_ci total_weight = 1; 674e5b75505Sopenharmony_ci 675e5b75505Sopenharmony_ci for (j = 1; j < n_chans; j++) { 676e5b75505Sopenharmony_ci adj_chan = acs_find_chan(iface, chan->freq + (j * 20)); 677e5b75505Sopenharmony_ci if (!adj_chan) 678e5b75505Sopenharmony_ci break; 679e5b75505Sopenharmony_ci 680e5b75505Sopenharmony_ci if (!chan_bw_allowed(adj_chan, bw, 1, 0)) { 681e5b75505Sopenharmony_ci wpa_printf(MSG_DEBUG, 682e5b75505Sopenharmony_ci "ACS: PRI Channel %d: secondary channel %d BW %u is not supported", 683e5b75505Sopenharmony_ci chan->chan, adj_chan->chan, bw); 684e5b75505Sopenharmony_ci break; 685e5b75505Sopenharmony_ci } 686e5b75505Sopenharmony_ci 687e5b75505Sopenharmony_ci if (acs_usable_chan(adj_chan)) { 688e5b75505Sopenharmony_ci factor += adj_chan->interference_factor; 689e5b75505Sopenharmony_ci total_weight += 1; 690e5b75505Sopenharmony_ci } 691e5b75505Sopenharmony_ci } 692e5b75505Sopenharmony_ci 693e5b75505Sopenharmony_ci if (j != n_chans) { 694e5b75505Sopenharmony_ci wpa_printf(MSG_DEBUG, "ACS: Channel %d: not enough bandwidth", 695e5b75505Sopenharmony_ci chan->chan); 696e5b75505Sopenharmony_ci continue; 697e5b75505Sopenharmony_ci } 698e5b75505Sopenharmony_ci 699e5b75505Sopenharmony_ci /* 2.4 GHz has overlapping 20 MHz channels. Include adjacent 700e5b75505Sopenharmony_ci * channel interference factor. */ 701e5b75505Sopenharmony_ci if (is_24ghz_mode(iface->current_mode->mode)) { 702e5b75505Sopenharmony_ci for (j = 0; j < n_chans; j++) { 703e5b75505Sopenharmony_ci adj_chan = acs_find_chan(iface, chan->freq + 704e5b75505Sopenharmony_ci (j * 20) - 5); 705e5b75505Sopenharmony_ci if (adj_chan && acs_usable_chan(adj_chan)) { 706e5b75505Sopenharmony_ci factor += ACS_ADJ_WEIGHT * 707e5b75505Sopenharmony_ci adj_chan->interference_factor; 708e5b75505Sopenharmony_ci total_weight += ACS_ADJ_WEIGHT; 709e5b75505Sopenharmony_ci } 710e5b75505Sopenharmony_ci 711e5b75505Sopenharmony_ci adj_chan = acs_find_chan(iface, chan->freq + 712e5b75505Sopenharmony_ci (j * 20) - 10); 713e5b75505Sopenharmony_ci if (adj_chan && acs_usable_chan(adj_chan)) { 714e5b75505Sopenharmony_ci factor += ACS_NEXT_ADJ_WEIGHT * 715e5b75505Sopenharmony_ci adj_chan->interference_factor; 716e5b75505Sopenharmony_ci total_weight += ACS_NEXT_ADJ_WEIGHT; 717e5b75505Sopenharmony_ci } 718e5b75505Sopenharmony_ci 719e5b75505Sopenharmony_ci adj_chan = acs_find_chan(iface, chan->freq + 720e5b75505Sopenharmony_ci (j * 20) + 5); 721e5b75505Sopenharmony_ci if (adj_chan && acs_usable_chan(adj_chan)) { 722e5b75505Sopenharmony_ci factor += ACS_ADJ_WEIGHT * 723e5b75505Sopenharmony_ci adj_chan->interference_factor; 724e5b75505Sopenharmony_ci total_weight += ACS_ADJ_WEIGHT; 725e5b75505Sopenharmony_ci } 726e5b75505Sopenharmony_ci 727e5b75505Sopenharmony_ci adj_chan = acs_find_chan(iface, chan->freq + 728e5b75505Sopenharmony_ci (j * 20) + 10); 729e5b75505Sopenharmony_ci if (adj_chan && acs_usable_chan(adj_chan)) { 730e5b75505Sopenharmony_ci factor += ACS_NEXT_ADJ_WEIGHT * 731e5b75505Sopenharmony_ci adj_chan->interference_factor; 732e5b75505Sopenharmony_ci total_weight += ACS_NEXT_ADJ_WEIGHT; 733e5b75505Sopenharmony_ci } 734e5b75505Sopenharmony_ci } 735e5b75505Sopenharmony_ci } 736e5b75505Sopenharmony_ci 737e5b75505Sopenharmony_ci factor /= total_weight; 738e5b75505Sopenharmony_ci 739e5b75505Sopenharmony_ci bias = NULL; 740e5b75505Sopenharmony_ci if (iface->conf->acs_chan_bias) { 741e5b75505Sopenharmony_ci for (k = 0; k < iface->conf->num_acs_chan_bias; k++) { 742e5b75505Sopenharmony_ci bias = &iface->conf->acs_chan_bias[k]; 743e5b75505Sopenharmony_ci if (bias->channel == chan->chan) 744e5b75505Sopenharmony_ci break; 745e5b75505Sopenharmony_ci bias = NULL; 746e5b75505Sopenharmony_ci } 747e5b75505Sopenharmony_ci } else if (is_24ghz_mode(iface->current_mode->mode) && 748e5b75505Sopenharmony_ci is_common_24ghz_chan(chan->chan)) { 749e5b75505Sopenharmony_ci tmp_bias.channel = chan->chan; 750e5b75505Sopenharmony_ci tmp_bias.bias = ACS_24GHZ_PREFER_1_6_11; 751e5b75505Sopenharmony_ci bias = &tmp_bias; 752e5b75505Sopenharmony_ci } 753e5b75505Sopenharmony_ci 754e5b75505Sopenharmony_ci if (bias) { 755e5b75505Sopenharmony_ci factor *= bias->bias; 756e5b75505Sopenharmony_ci wpa_printf(MSG_DEBUG, 757e5b75505Sopenharmony_ci "ACS: * channel %d: total interference = %Lg (%f bias)", 758e5b75505Sopenharmony_ci chan->chan, factor, bias->bias); 759e5b75505Sopenharmony_ci } else { 760e5b75505Sopenharmony_ci wpa_printf(MSG_DEBUG, 761e5b75505Sopenharmony_ci "ACS: * channel %d: total interference = %Lg", 762e5b75505Sopenharmony_ci chan->chan, factor); 763e5b75505Sopenharmony_ci } 764e5b75505Sopenharmony_ci 765e5b75505Sopenharmony_ci if (acs_usable_chan(chan) && 766e5b75505Sopenharmony_ci (!ideal_chan || factor < ideal_factor)) { 767e5b75505Sopenharmony_ci ideal_factor = factor; 768e5b75505Sopenharmony_ci ideal_chan = chan; 769e5b75505Sopenharmony_ci } 770e5b75505Sopenharmony_ci 771e5b75505Sopenharmony_ci /* This channel would at least be usable */ 772e5b75505Sopenharmony_ci if (!rand_chan) 773e5b75505Sopenharmony_ci rand_chan = chan; 774e5b75505Sopenharmony_ci } 775e5b75505Sopenharmony_ci 776e5b75505Sopenharmony_ci if (ideal_chan) { 777e5b75505Sopenharmony_ci wpa_printf(MSG_DEBUG, "ACS: Ideal channel is %d (%d MHz) with total interference factor of %Lg", 778e5b75505Sopenharmony_ci ideal_chan->chan, ideal_chan->freq, ideal_factor); 779e5b75505Sopenharmony_ci return ideal_chan; 780e5b75505Sopenharmony_ci } 781e5b75505Sopenharmony_ci 782e5b75505Sopenharmony_ci return rand_chan; 783e5b75505Sopenharmony_ci} 784e5b75505Sopenharmony_ci 785e5b75505Sopenharmony_ci 786e5b75505Sopenharmony_cistatic void acs_adjust_center_freq(struct hostapd_iface *iface) 787e5b75505Sopenharmony_ci{ 788e5b75505Sopenharmony_ci int offset; 789e5b75505Sopenharmony_ci 790e5b75505Sopenharmony_ci wpa_printf(MSG_DEBUG, "ACS: Adjusting VHT center frequency"); 791e5b75505Sopenharmony_ci 792e5b75505Sopenharmony_ci switch (hostapd_get_oper_chwidth(iface->conf)) { 793e5b75505Sopenharmony_ci case CHANWIDTH_USE_HT: 794e5b75505Sopenharmony_ci offset = 2 * iface->conf->secondary_channel; 795e5b75505Sopenharmony_ci break; 796e5b75505Sopenharmony_ci case CHANWIDTH_80MHZ: 797e5b75505Sopenharmony_ci offset = 6; 798e5b75505Sopenharmony_ci break; 799e5b75505Sopenharmony_ci case CHANWIDTH_160MHZ: 800e5b75505Sopenharmony_ci offset = 14; 801e5b75505Sopenharmony_ci break; 802e5b75505Sopenharmony_ci default: 803e5b75505Sopenharmony_ci /* TODO: How can this be calculated? Adjust 804e5b75505Sopenharmony_ci * acs_find_ideal_chan() */ 805e5b75505Sopenharmony_ci wpa_printf(MSG_INFO, 806e5b75505Sopenharmony_ci "ACS: Only VHT20/40/80/160 is supported now"); 807e5b75505Sopenharmony_ci return; 808e5b75505Sopenharmony_ci } 809e5b75505Sopenharmony_ci 810e5b75505Sopenharmony_ci hostapd_set_oper_centr_freq_seg0_idx(iface->conf, 811e5b75505Sopenharmony_ci iface->conf->channel + offset); 812e5b75505Sopenharmony_ci} 813e5b75505Sopenharmony_ci 814e5b75505Sopenharmony_ci 815e5b75505Sopenharmony_cistatic int acs_study_survey_based(struct hostapd_iface *iface) 816e5b75505Sopenharmony_ci{ 817e5b75505Sopenharmony_ci wpa_printf(MSG_DEBUG, "ACS: Trying survey-based ACS"); 818e5b75505Sopenharmony_ci 819e5b75505Sopenharmony_ci if (!iface->chans_surveyed) { 820e5b75505Sopenharmony_ci wpa_printf(MSG_ERROR, "ACS: Unable to collect survey data"); 821e5b75505Sopenharmony_ci return -1; 822e5b75505Sopenharmony_ci } 823e5b75505Sopenharmony_ci 824e5b75505Sopenharmony_ci if (!acs_surveys_are_sufficient(iface)) { 825e5b75505Sopenharmony_ci wpa_printf(MSG_ERROR, "ACS: Surveys have insufficient data"); 826e5b75505Sopenharmony_ci return -1; 827e5b75505Sopenharmony_ci } 828e5b75505Sopenharmony_ci 829e5b75505Sopenharmony_ci acs_survey_all_chans_intereference_factor(iface); 830e5b75505Sopenharmony_ci return 0; 831e5b75505Sopenharmony_ci} 832e5b75505Sopenharmony_ci 833e5b75505Sopenharmony_ci 834e5b75505Sopenharmony_cistatic int acs_study_options(struct hostapd_iface *iface) 835e5b75505Sopenharmony_ci{ 836e5b75505Sopenharmony_ci if (acs_study_survey_based(iface) == 0) 837e5b75505Sopenharmony_ci return 0; 838e5b75505Sopenharmony_ci 839e5b75505Sopenharmony_ci /* TODO: If no surveys are available/sufficient this is a good 840e5b75505Sopenharmony_ci * place to fallback to BSS-based ACS */ 841e5b75505Sopenharmony_ci 842e5b75505Sopenharmony_ci return -1; 843e5b75505Sopenharmony_ci} 844e5b75505Sopenharmony_ci 845e5b75505Sopenharmony_ci 846e5b75505Sopenharmony_cistatic void acs_study(struct hostapd_iface *iface) 847e5b75505Sopenharmony_ci{ 848e5b75505Sopenharmony_ci struct hostapd_channel_data *ideal_chan; 849e5b75505Sopenharmony_ci int err; 850e5b75505Sopenharmony_ci 851e5b75505Sopenharmony_ci err = acs_study_options(iface); 852e5b75505Sopenharmony_ci if (err < 0) { 853e5b75505Sopenharmony_ci wpa_printf(MSG_ERROR, "ACS: All study options have failed"); 854e5b75505Sopenharmony_ci goto fail; 855e5b75505Sopenharmony_ci } 856e5b75505Sopenharmony_ci 857e5b75505Sopenharmony_ci ideal_chan = acs_find_ideal_chan(iface); 858e5b75505Sopenharmony_ci if (!ideal_chan) { 859e5b75505Sopenharmony_ci wpa_printf(MSG_ERROR, "ACS: Failed to compute ideal channel"); 860e5b75505Sopenharmony_ci err = -1; 861e5b75505Sopenharmony_ci goto fail; 862e5b75505Sopenharmony_ci } 863e5b75505Sopenharmony_ci 864e5b75505Sopenharmony_ci iface->conf->channel = ideal_chan->chan; 865e5b75505Sopenharmony_ci 866e5b75505Sopenharmony_ci if (iface->conf->ieee80211ac || iface->conf->ieee80211ax) 867e5b75505Sopenharmony_ci acs_adjust_center_freq(iface); 868e5b75505Sopenharmony_ci 869e5b75505Sopenharmony_ci err = 0; 870e5b75505Sopenharmony_cifail: 871e5b75505Sopenharmony_ci /* 872e5b75505Sopenharmony_ci * hostapd_setup_interface_complete() will return -1 on failure, 873e5b75505Sopenharmony_ci * 0 on success and 0 is HOSTAPD_CHAN_VALID :) 874e5b75505Sopenharmony_ci */ 875e5b75505Sopenharmony_ci if (hostapd_acs_completed(iface, err) == HOSTAPD_CHAN_VALID) { 876e5b75505Sopenharmony_ci acs_cleanup(iface); 877e5b75505Sopenharmony_ci return; 878e5b75505Sopenharmony_ci } 879e5b75505Sopenharmony_ci 880e5b75505Sopenharmony_ci /* This can possibly happen if channel parameters (secondary 881e5b75505Sopenharmony_ci * channel, center frequencies) are misconfigured */ 882e5b75505Sopenharmony_ci wpa_printf(MSG_ERROR, "ACS: Possibly channel configuration is invalid, please report this along with your config file."); 883e5b75505Sopenharmony_ci acs_fail(iface); 884e5b75505Sopenharmony_ci} 885e5b75505Sopenharmony_ci 886e5b75505Sopenharmony_ci 887e5b75505Sopenharmony_cistatic void acs_scan_complete(struct hostapd_iface *iface) 888e5b75505Sopenharmony_ci{ 889e5b75505Sopenharmony_ci int err; 890e5b75505Sopenharmony_ci 891e5b75505Sopenharmony_ci iface->scan_cb = NULL; 892e5b75505Sopenharmony_ci 893e5b75505Sopenharmony_ci wpa_printf(MSG_DEBUG, "ACS: Using survey based algorithm (acs_num_scans=%d)", 894e5b75505Sopenharmony_ci iface->conf->acs_num_scans); 895e5b75505Sopenharmony_ci 896e5b75505Sopenharmony_ci err = hostapd_drv_get_survey(iface->bss[0], 0); 897e5b75505Sopenharmony_ci if (err) { 898e5b75505Sopenharmony_ci wpa_printf(MSG_ERROR, "ACS: Failed to get survey data"); 899e5b75505Sopenharmony_ci goto fail; 900e5b75505Sopenharmony_ci } 901e5b75505Sopenharmony_ci 902e5b75505Sopenharmony_ci if (++iface->acs_num_completed_scans < iface->conf->acs_num_scans) { 903e5b75505Sopenharmony_ci err = acs_request_scan(iface); 904e5b75505Sopenharmony_ci if (err) { 905e5b75505Sopenharmony_ci wpa_printf(MSG_ERROR, "ACS: Failed to request scan"); 906e5b75505Sopenharmony_ci goto fail; 907e5b75505Sopenharmony_ci } 908e5b75505Sopenharmony_ci 909e5b75505Sopenharmony_ci return; 910e5b75505Sopenharmony_ci } 911e5b75505Sopenharmony_ci 912e5b75505Sopenharmony_ci acs_study(iface); 913e5b75505Sopenharmony_ci return; 914e5b75505Sopenharmony_cifail: 915e5b75505Sopenharmony_ci hostapd_acs_completed(iface, 1); 916e5b75505Sopenharmony_ci acs_fail(iface); 917e5b75505Sopenharmony_ci} 918e5b75505Sopenharmony_ci 919e5b75505Sopenharmony_ci 920e5b75505Sopenharmony_cistatic int acs_request_scan(struct hostapd_iface *iface) 921e5b75505Sopenharmony_ci{ 922e5b75505Sopenharmony_ci struct wpa_driver_scan_params params; 923e5b75505Sopenharmony_ci struct hostapd_channel_data *chan; 924e5b75505Sopenharmony_ci int i, *freq; 925e5b75505Sopenharmony_ci 926e5b75505Sopenharmony_ci os_memset(¶ms, 0, sizeof(params)); 927e5b75505Sopenharmony_ci params.freqs = os_calloc(iface->current_mode->num_channels + 1, 928e5b75505Sopenharmony_ci sizeof(params.freqs[0])); 929e5b75505Sopenharmony_ci if (params.freqs == NULL) 930e5b75505Sopenharmony_ci return -1; 931e5b75505Sopenharmony_ci 932e5b75505Sopenharmony_ci freq = params.freqs; 933e5b75505Sopenharmony_ci for (i = 0; i < iface->current_mode->num_channels; i++) { 934e5b75505Sopenharmony_ci chan = &iface->current_mode->channels[i]; 935e5b75505Sopenharmony_ci if (chan->flag & HOSTAPD_CHAN_DISABLED) 936e5b75505Sopenharmony_ci continue; 937e5b75505Sopenharmony_ci 938e5b75505Sopenharmony_ci if (!is_in_chanlist(iface, chan)) 939e5b75505Sopenharmony_ci continue; 940e5b75505Sopenharmony_ci 941e5b75505Sopenharmony_ci *freq++ = chan->freq; 942e5b75505Sopenharmony_ci } 943e5b75505Sopenharmony_ci *freq = 0; 944e5b75505Sopenharmony_ci 945e5b75505Sopenharmony_ci iface->scan_cb = acs_scan_complete; 946e5b75505Sopenharmony_ci 947e5b75505Sopenharmony_ci wpa_printf(MSG_DEBUG, "ACS: Scanning %d / %d", 948e5b75505Sopenharmony_ci iface->acs_num_completed_scans + 1, 949e5b75505Sopenharmony_ci iface->conf->acs_num_scans); 950e5b75505Sopenharmony_ci 951e5b75505Sopenharmony_ci if (hostapd_driver_scan(iface->bss[0], ¶ms) < 0) { 952e5b75505Sopenharmony_ci wpa_printf(MSG_ERROR, "ACS: Failed to request initial scan"); 953e5b75505Sopenharmony_ci acs_cleanup(iface); 954e5b75505Sopenharmony_ci os_free(params.freqs); 955e5b75505Sopenharmony_ci return -1; 956e5b75505Sopenharmony_ci } 957e5b75505Sopenharmony_ci 958e5b75505Sopenharmony_ci os_free(params.freqs); 959e5b75505Sopenharmony_ci return 0; 960e5b75505Sopenharmony_ci} 961e5b75505Sopenharmony_ci 962e5b75505Sopenharmony_ci 963e5b75505Sopenharmony_cienum hostapd_chan_status acs_init(struct hostapd_iface *iface) 964e5b75505Sopenharmony_ci{ 965e5b75505Sopenharmony_ci wpa_printf(MSG_INFO, "ACS: Automatic channel selection started, this may take a bit"); 966e5b75505Sopenharmony_ci 967e5b75505Sopenharmony_ci if (iface->drv_flags & WPA_DRIVER_FLAGS_ACS_OFFLOAD) { 968e5b75505Sopenharmony_ci wpa_printf(MSG_INFO, "ACS: Offloading to driver"); 969e5b75505Sopenharmony_ci if (hostapd_drv_do_acs(iface->bss[0])) 970e5b75505Sopenharmony_ci return HOSTAPD_CHAN_INVALID; 971e5b75505Sopenharmony_ci return HOSTAPD_CHAN_ACS; 972e5b75505Sopenharmony_ci } 973e5b75505Sopenharmony_ci 974e5b75505Sopenharmony_ci if (!iface->current_mode) 975e5b75505Sopenharmony_ci return HOSTAPD_CHAN_INVALID; 976e5b75505Sopenharmony_ci 977e5b75505Sopenharmony_ci acs_cleanup(iface); 978e5b75505Sopenharmony_ci 979e5b75505Sopenharmony_ci if (acs_request_scan(iface) < 0) 980e5b75505Sopenharmony_ci return HOSTAPD_CHAN_INVALID; 981e5b75505Sopenharmony_ci 982e5b75505Sopenharmony_ci hostapd_set_state(iface, HAPD_IFACE_ACS); 983e5b75505Sopenharmony_ci wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, ACS_EVENT_STARTED); 984e5b75505Sopenharmony_ci 985e5b75505Sopenharmony_ci return HOSTAPD_CHAN_ACS; 986e5b75505Sopenharmony_ci} 987