Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 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" |
Sunil Ravi | 7f76929 | 2024-07-23 22:21:32 +0000 | [diff] [blame] | 15 | #include "utils/eloop.h" |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 16 | #include "common/ieee802_11_defs.h" |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 17 | #include "common/hw_features_common.h" |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 18 | #include "common/wpa_ctrl.h" |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 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 | * --- |
Sunil Ravi | 7f76929 | 2024-07-23 22:21:32 +0000 | [diff] [blame] | 79 | * (busy time - tx time) / (active time - tx time) * 2^(chan_nf - band_min_nf) |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 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) * |
Sunil Ravi | 7f76929 | 2024-07-23 22:21:32 +0000 | [diff] [blame] | 96 | * 2^(10^(chan_nf/10) - 10^(band_min_nf/10)) |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 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) * |
Sunil Ravi | 7f76929 | 2024-07-23 22:21:32 +0000 | [diff] [blame] | 104 | * 2^(10^(chan_nf/10) - 10^(band_min_nf/10)) |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 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 | |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 245 | enum bw_type { |
| 246 | ACS_BW40, |
| 247 | ACS_BW80, |
| 248 | ACS_BW160, |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 249 | ACS_BW320_1, |
| 250 | ACS_BW320_2, |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 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 }, |
Sunil Ravi | 2a14cf1 | 2023-11-21 00:54:38 +0000 | [diff] [blame] | 262 | { 5580, 5600, 118 }, { 5620, 5640, 126 }, { 5660, 5680, 134 }, |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 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 | }; |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 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 | }; |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 300 | static const struct bw_item *bw_desc[] = { |
| 301 | [ACS_BW40] = bw_40, |
| 302 | [ACS_BW80] = bw_80, |
| 303 | [ACS_BW160] = bw_160, |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 304 | [ACS_BW320_1] = bw_320_1, |
| 305 | [ACS_BW320_2] = bw_320_2, |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 306 | }; |
| 307 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 308 | |
| 309 | static int acs_request_scan(struct hostapd_iface *iface); |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 310 | static int acs_survey_is_sufficient(struct freq_survey *survey); |
Sunil Ravi | 7f76929 | 2024-07-23 22:21:32 +0000 | [diff] [blame] | 311 | static void acs_scan_retry(void *eloop_data, void *user_data); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 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 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 329 | static void acs_cleanup_mode(struct hostapd_hw_modes *mode) |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 330 | { |
| 331 | int i; |
| 332 | struct hostapd_channel_data *chan; |
| 333 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 334 | for (i = 0; i < mode->num_channels; i++) { |
| 335 | chan = &mode->channels[i]; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 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; |
Sunil Ravi | af8751c | 2023-03-29 11:35:17 -0700 | [diff] [blame] | 343 | chan->punct_bitmap = 0; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 344 | } |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 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]); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 354 | |
| 355 | iface->chans_surveyed = 0; |
| 356 | iface->acs_num_completed_scans = 0; |
Sunil Ravi | 7f76929 | 2024-07-23 22:21:32 +0000 | [diff] [blame] | 357 | iface->acs_num_retries = 0; |
| 358 | eloop_cancel_timeout(acs_scan_retry, iface, NULL); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 362 | static void acs_fail(struct hostapd_iface *iface) |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 363 | { |
| 364 | wpa_printf(MSG_ERROR, "ACS: Failed to start"); |
| 365 | acs_cleanup(iface); |
Dmitry Shmidt | 2ac5f60 | 2014-03-07 10:08:21 -0800 | [diff] [blame] | 366 | hostapd_disable_iface(iface); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 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 { |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 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) + |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 393 | (total ? (busy / total) : 0) * |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 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; |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 408 | unsigned count = 0; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 409 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 410 | if (dl_list_empty(&chan->survey_list) || |
| 411 | (chan->flag & HOSTAPD_CHAN_DISABLED)) |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 412 | return; |
| 413 | |
| 414 | chan->interference_factor = 0; |
| 415 | |
| 416 | dl_list_for_each(survey, &chan->survey_list, struct freq_survey, list) |
| 417 | { |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 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++; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 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", |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 430 | i, chan->min_nf, int_factor, |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 431 | survey->nf, (unsigned long) survey->channel_time, |
| 432 | (unsigned long) survey->channel_time_busy, |
| 433 | (unsigned long) survey->channel_time_rx); |
| 434 | } |
| 435 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 436 | if (count) |
| 437 | chan->interference_factor /= count; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 441 | static bool acs_usable_bw_chan(const struct hostapd_channel_data *chan, |
| 442 | enum bw_type bw) |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 443 | { |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 444 | unsigned int i = 0; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 445 | |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 446 | while (bw_desc[bw][i].first != -1) { |
| 447 | if (chan->freq == bw_desc[bw][i].first) |
| 448 | return true; |
| 449 | i++; |
| 450 | } |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 451 | |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 452 | return false; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 456 | static int acs_get_bw_center_chan(int freq, enum bw_type bw) |
Dmitry Shmidt | a38abf9 | 2014-03-06 13:38:44 -0800 | [diff] [blame] | 457 | { |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 458 | unsigned int i = 0; |
Dmitry Shmidt | a38abf9 | 2014-03-06 13:38:44 -0800 | [diff] [blame] | 459 | |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 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 | } |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 466 | |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 471 | static int acs_survey_is_sufficient(struct freq_survey *survey) |
| 472 | { |
| 473 | if (!(survey->filled & SURVEY_HAS_NF)) { |
Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame] | 474 | wpa_printf(MSG_INFO, |
| 475 | "ACS: Survey for freq %d is missing noise floor", |
| 476 | survey->freq); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | if (!(survey->filled & SURVEY_HAS_CHAN_TIME)) { |
Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame] | 481 | wpa_printf(MSG_INFO, |
| 482 | "ACS: Survey for freq %d is missing channel time", |
| 483 | survey->freq); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | if (!(survey->filled & SURVEY_HAS_CHAN_TIME_BUSY) && |
| 488 | !(survey->filled & SURVEY_HAS_CHAN_TIME_RX)) { |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 489 | wpa_printf(MSG_INFO, |
Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame] | 490 | "ACS: Survey for freq %d is missing RX and busy time (at least one is required)", |
| 491 | survey->freq); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | return 1; |
| 496 | } |
| 497 | |
| 498 | |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 499 | static int acs_survey_list_is_sufficient(struct hostapd_channel_data *chan) |
| 500 | { |
| 501 | struct freq_survey *survey; |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 502 | int ret = -1; |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 503 | |
| 504 | dl_list_for_each(survey, &chan->survey_list, struct freq_survey, list) |
| 505 | { |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 506 | if (acs_survey_is_sufficient(survey)) { |
| 507 | ret = 1; |
| 508 | break; |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 509 | } |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 510 | ret = 0; |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 513 | if (ret == -1) |
Sunil Ravi | 7f76929 | 2024-07-23 22:21:32 +0000 | [diff] [blame] | 514 | ret = 0; /* no survey list entries */ |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 515 | |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 516 | if (!ret) { |
| 517 | wpa_printf(MSG_INFO, |
| 518 | "ACS: Channel %d has insufficient survey data", |
| 519 | chan->chan); |
| 520 | } |
| 521 | |
| 522 | return ret; |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 526 | static int acs_surveys_are_sufficient_mode(struct hostapd_hw_modes *mode) |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 527 | { |
| 528 | int i; |
| 529 | struct hostapd_channel_data *chan; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 530 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 531 | for (i = 0; i < mode->num_channels; i++) { |
| 532 | chan = &mode->channels[i]; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 533 | if (!(chan->flag & HOSTAPD_CHAN_DISABLED) && |
| 534 | acs_survey_list_is_sufficient(chan)) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 535 | return 1; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 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; |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | |
| 558 | static int acs_usable_chan(struct hostapd_channel_data *chan) |
| 559 | { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 560 | return !dl_list_empty(&chan->survey_list) && |
| 561 | !(chan->flag & HOSTAPD_CHAN_DISABLED) && |
| 562 | acs_survey_list_is_sufficient(chan); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 566 | static int is_in_chanlist(struct hostapd_iface *iface, |
| 567 | struct hostapd_channel_data *chan) |
| 568 | { |
Dmitry Shmidt | dda10c2 | 2015-03-24 16:05:01 -0700 | [diff] [blame] | 569 | if (!iface->conf->acs_ch_list.num) |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 570 | return 1; |
| 571 | |
Dmitry Shmidt | dda10c2 | 2015-03-24 16:05:01 -0700 | [diff] [blame] | 572 | return freq_range_list_includes(&iface->conf->acs_ch_list, chan->chan); |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame] | 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 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 587 | static void acs_survey_mode_interference_factor( |
| 588 | struct hostapd_iface *iface, struct hostapd_hw_modes *mode) |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 589 | { |
| 590 | int i; |
| 591 | struct hostapd_channel_data *chan; |
| 592 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 593 | for (i = 0; i < mode->num_channels; i++) { |
| 594 | chan = &mode->channels[i]; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 595 | |
| 596 | if (!acs_usable_chan(chan)) |
| 597 | continue; |
| 598 | |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 599 | if ((chan->flag & HOSTAPD_CHAN_RADAR) && |
| 600 | iface->conf->acs_exclude_dfs) |
| 601 | continue; |
| 602 | |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 603 | if (!is_in_chanlist(iface, chan)) |
| 604 | continue; |
| 605 | |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame] | 606 | if (!is_in_freqlist(iface, chan)) |
| 607 | continue; |
| 608 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 609 | if (chan->max_tx_power < iface->conf->min_tx_power) |
| 610 | continue; |
| 611 | |
Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame] | 612 | if ((chan->flag & HOSTAPD_CHAN_INDOOR_ONLY) && |
| 613 | iface->conf->country[2] == 0x4f) |
| 614 | continue; |
| 615 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 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 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 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) |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 643 | { |
| 644 | struct hostapd_channel_data *chan; |
| 645 | int i; |
| 646 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 647 | for (i = 0; i < mode->num_channels; i++) { |
| 648 | chan = &mode->channels[i]; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 649 | |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 650 | if (chan->flag & HOSTAPD_CHAN_DISABLED) |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 651 | continue; |
| 652 | |
| 653 | if (chan->freq == freq) |
| 654 | return chan; |
| 655 | } |
| 656 | |
| 657 | return NULL; |
| 658 | } |
| 659 | |
| 660 | |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 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 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 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 | |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 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 | |
Sunil Ravi | af8751c | 2023-03-29 11:35:17 -0700 | [diff] [blame] | 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 | |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 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 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 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) |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 830 | { |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 831 | struct hostapd_channel_data *chan, *adj_chan = NULL, *best; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 832 | long double factor; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 833 | int i, j; |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 834 | int bw320_offset = 0, ideal_bw320_offset = 0; |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 835 | unsigned int k; |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 836 | int secondary_channel = 1, freq_offset; |
Sunil Ravi | 7f76929 | 2024-07-23 22:21:32 +0000 | [diff] [blame] | 837 | #ifdef CONFIG_IEEE80211BE |
| 838 | int index_primary = 0; |
| 839 | #endif /* CONFIG_IEEE80211BE */ |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 840 | |
| 841 | if (is_24ghz_mode(mode->mode)) |
| 842 | secondary_channel = iface->conf->secondary_channel; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 843 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 844 | for (i = 0; i < mode->num_channels; i++) { |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 845 | double total_weight = 0; |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 846 | struct acs_bias *bias, tmp_bias; |
| 847 | |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 848 | chan = &mode->channels[i]; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 849 | |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 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 |
Sunil Ravi | af8751c | 2023-03-29 11:35:17 -0700 | [diff] [blame] | 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 | */ |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 860 | if (!chan_pri_allowed(chan)) |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 861 | continue; |
| 862 | |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 863 | if ((chan->flag & HOSTAPD_CHAN_RADAR) && |
| 864 | iface->conf->acs_exclude_dfs) |
| 865 | continue; |
| 866 | |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 867 | if (!is_in_chanlist(iface, chan)) |
| 868 | continue; |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 869 | |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame] | 870 | if (!is_in_freqlist(iface, chan)) |
| 871 | continue; |
| 872 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 873 | if (chan->max_tx_power < iface->conf->min_tx_power) |
| 874 | continue; |
| 875 | |
Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame] | 876 | if ((chan->flag & HOSTAPD_CHAN_INDOOR_ONLY) && |
| 877 | iface->conf->country[2] == 0x4f) |
| 878 | continue; |
| 879 | |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 880 | if (!chan_bw_allowed(chan, bw, secondary_channel != -1, 1)) { |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 881 | wpa_printf(MSG_DEBUG, |
| 882 | "ACS: Channel %d: BW %u is not supported", |
| 883 | chan->chan, bw); |
| 884 | continue; |
| 885 | } |
| 886 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 887 | /* HT40 on 5 GHz has a limited set of primary channels as per |
| 888 | * 11n Annex J */ |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 889 | if (mode->mode == HOSTAPD_MODE_IEEE80211A && |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 890 | ((iface->conf->ieee80211n && |
| 891 | iface->conf->secondary_channel) || |
| 892 | is_6ghz_freq(chan->freq)) && |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 893 | !acs_usable_bw_chan(chan, ACS_BW40)) { |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 894 | wpa_printf(MSG_DEBUG, |
| 895 | "ACS: Channel %d: not allowed as primary channel for 40 MHz bandwidth", |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 896 | chan->chan); |
| 897 | continue; |
| 898 | } |
| 899 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 900 | if (mode->mode == HOSTAPD_MODE_IEEE80211A && |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 901 | (iface->conf->ieee80211ac || iface->conf->ieee80211ax || |
| 902 | iface->conf->ieee80211be)) { |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 903 | if (hostapd_get_oper_chwidth(iface->conf) == |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 904 | CONF_OPER_CHWIDTH_80MHZ && |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 905 | !acs_usable_bw_chan(chan, ACS_BW80)) { |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 906 | wpa_printf(MSG_DEBUG, |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 907 | "ACS: Channel %d: not allowed as primary channel for 80 MHz bandwidth", |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 908 | chan->chan); |
| 909 | continue; |
| 910 | } |
| 911 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 912 | if (hostapd_get_oper_chwidth(iface->conf) == |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 913 | CONF_OPER_CHWIDTH_160MHZ && |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 914 | !acs_usable_bw_chan(chan, ACS_BW160)) { |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 915 | wpa_printf(MSG_DEBUG, |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 916 | "ACS: Channel %d: not allowed as primary channel for 160 MHz bandwidth", |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 917 | chan->chan); |
| 918 | continue; |
| 919 | } |
Dmitry Shmidt | a38abf9 | 2014-03-06 13:38:44 -0800 | [diff] [blame] | 920 | } |
| 921 | |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 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 | |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 930 | factor = 0; |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 931 | best = NULL; |
| 932 | if (acs_usable_chan(chan)) { |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 933 | factor = chan->interference_factor; |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 934 | total_weight = 1; |
| 935 | best = chan; |
| 936 | } |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 937 | |
| 938 | for (j = 1; j < n_chans; j++) { |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 939 | adj_chan = acs_find_chan(iface, chan->freq + |
| 940 | j * secondary_channel * 20); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 941 | if (!adj_chan) |
| 942 | break; |
| 943 | |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 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 | |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 951 | if (!acs_usable_chan(adj_chan)) |
| 952 | continue; |
| 953 | |
| 954 | factor += adj_chan->interference_factor; |
| 955 | total_weight += 1; |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 956 | |
| 957 | /* find the best channel in this segment */ |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 958 | if (!best || adj_chan->interference_factor < |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 959 | best->interference_factor) |
| 960 | best = adj_chan; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 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 | |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 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 */ |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 971 | if (iface->current_mode && |
| 972 | iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A && |
| 973 | best && chan != best) { |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 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); |
Sunil Ravi | 7f76929 | 2024-07-23 22:21:32 +0000 | [diff] [blame] | 979 | #ifdef CONFIG_IEEE80211BE |
| 980 | index_primary = (chan->freq - best->freq) / 20; |
| 981 | #endif /* CONFIG_IEEE80211BE */ |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 982 | chan = best; |
| 983 | } |
| 984 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 985 | /* 2.4 GHz has overlapping 20 MHz channels. Include adjacent |
| 986 | * channel interference factor. */ |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 987 | if (is_24ghz_mode(mode->mode)) { |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 988 | for (j = 0; j < n_chans; j++) { |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 989 | freq_offset = j * 20 * secondary_channel; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 990 | adj_chan = acs_find_chan(iface, chan->freq + |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 991 | freq_offset - 5); |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 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 | } |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 997 | |
| 998 | adj_chan = acs_find_chan(iface, chan->freq + |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 999 | freq_offset - 10); |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 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 | } |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1005 | |
| 1006 | adj_chan = acs_find_chan(iface, chan->freq + |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 1007 | freq_offset + 5); |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 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 | } |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1013 | |
| 1014 | adj_chan = acs_find_chan(iface, chan->freq + |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 1015 | freq_offset + 10); |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 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 | } |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1021 | } |
| 1022 | } |
| 1023 | |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 1024 | if (total_weight == 0) |
| 1025 | continue; |
| 1026 | |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 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 | } |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1037 | } else if (is_24ghz_mode(mode->mode) && |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 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 | } |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1054 | |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 1055 | if (acs_usable_chan(chan) && |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1056 | (!*ideal_chan || factor < *ideal_factor)) { |
Sunil Ravi | af8751c | 2023-03-29 11:35:17 -0700 | [diff] [blame] | 1057 | /* Reset puncturing bitmap for the previous ideal |
| 1058 | * channel */ |
| 1059 | if (*ideal_chan) |
| 1060 | (*ideal_chan)->punct_bitmap = 0; |
| 1061 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1062 | *ideal_factor = factor; |
| 1063 | *ideal_chan = chan; |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 1064 | ideal_bw320_offset = bw320_offset; |
Sunil Ravi | af8751c | 2023-03-29 11:35:17 -0700 | [diff] [blame] | 1065 | |
| 1066 | #ifdef CONFIG_IEEE80211BE |
| 1067 | if (iface->conf->ieee80211be) |
| 1068 | acs_update_puncturing_bitmap(iface, mode, bw, |
| 1069 | n_chans, chan, |
Sunil Ravi | 7f76929 | 2024-07-23 22:21:32 +0000 | [diff] [blame] | 1070 | factor, |
| 1071 | index_primary); |
Sunil Ravi | af8751c | 2023-03-29 11:35:17 -0700 | [diff] [blame] | 1072 | #endif /* CONFIG_IEEE80211BE */ |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1073 | } |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 1074 | |
| 1075 | /* This channel would at least be usable */ |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 1076 | if (!(*rand_chan)) { |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1077 | *rand_chan = chan; |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 1078 | ideal_bw320_offset = bw320_offset; |
| 1079 | } |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1080 | } |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 1081 | |
| 1082 | hostapd_set_and_check_bw320_offset(iface->conf, ideal_bw320_offset); |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 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 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 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 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1109 | if (iface->conf->ieee80211n && |
| 1110 | iface->conf->secondary_channel) |
| 1111 | n_chans = 2; |
| 1112 | |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 1113 | if (iface->conf->ieee80211ac || iface->conf->ieee80211ax || |
| 1114 | iface->conf->ieee80211be) { |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1115 | switch (hostapd_get_oper_chwidth(iface->conf)) { |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 1116 | case CONF_OPER_CHWIDTH_80MHZ: |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1117 | n_chans = 4; |
| 1118 | break; |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 1119 | case CONF_OPER_CHWIDTH_160MHZ: |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1120 | n_chans = 8; |
| 1121 | break; |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 1122 | case CONF_OPER_CHWIDTH_320MHZ: |
| 1123 | n_chans = 16; |
| 1124 | break; |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 1125 | default: |
| 1126 | break; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | bw = num_chan_to_bw(n_chans); |
| 1131 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 1132 | bw_selected: |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 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); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1144 | } |
| 1145 | |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 1146 | if (ideal_chan) { |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 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); |
Sunil Ravi | af8751c | 2023-03-29 11:35:17 -0700 | [diff] [blame] | 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 | |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 1156 | return ideal_chan; |
| 1157 | } |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1158 | |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 1159 | return rand_chan; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 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 | |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 1174 | wpa_printf(MSG_DEBUG, |
| 1175 | "ACS: Adjusting HT/VHT/HE/EHT secondary frequency"); |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 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 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1186 | static void acs_adjust_center_freq(struct hostapd_iface *iface) |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1187 | { |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 1188 | int center; |
Dmitry Shmidt | b36ed7c | 2014-03-17 10:57:26 -0700 | [diff] [blame] | 1189 | |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 1190 | wpa_printf(MSG_DEBUG, "ACS: Adjusting center frequency"); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1191 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1192 | switch (hostapd_get_oper_chwidth(iface->conf)) { |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 1193 | case CONF_OPER_CHWIDTH_USE_HT: |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 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; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1202 | break; |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 1203 | case CONF_OPER_CHWIDTH_80MHZ: |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 1204 | center = acs_get_bw_center_chan(iface->freq, ACS_BW80); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1205 | break; |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 1206 | case CONF_OPER_CHWIDTH_160MHZ: |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 1207 | center = acs_get_bw_center_chan(iface->freq, ACS_BW160); |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 1208 | break; |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 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; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1226 | default: |
| 1227 | /* TODO: How can this be calculated? Adjust |
| 1228 | * acs_find_ideal_chan() */ |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 1229 | wpa_printf(MSG_INFO, |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 1230 | "ACS: Only VHT20/40/80/160/320 is supported now"); |
Dmitry Shmidt | b36ed7c | 2014-03-17 10:57:26 -0700 | [diff] [blame] | 1231 | return; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1232 | } |
Dmitry Shmidt | b36ed7c | 2014-03-17 10:57:26 -0700 | [diff] [blame] | 1233 | |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 1234 | hostapd_set_oper_centr_freq_seg0_idx(iface->conf, center); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 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 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1252 | acs_survey_all_chans_interference_factor(iface); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1253 | return 0; |
| 1254 | } |
| 1255 | |
| 1256 | |
| 1257 | static int acs_study_options(struct hostapd_iface *iface) |
| 1258 | { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1259 | if (acs_study_survey_based(iface) == 0) |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 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"); |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 1283 | err = -1; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1284 | goto fail; |
| 1285 | } |
| 1286 | |
| 1287 | iface->conf->channel = ideal_chan->chan; |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 1288 | iface->freq = ideal_chan->freq; |
Sunil Ravi | af8751c | 2023-03-29 11:35:17 -0700 | [diff] [blame] | 1289 | #ifdef CONFIG_IEEE80211BE |
| 1290 | iface->conf->punct_bitmap = ideal_chan->punct_bitmap; |
| 1291 | #endif /* CONFIG_IEEE80211BE */ |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1292 | |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 1293 | if (iface->conf->ieee80211ac || iface->conf->ieee80211ax || |
| 1294 | iface->conf->ieee80211be) { |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 1295 | acs_adjust_secondary(iface); |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1296 | acs_adjust_center_freq(iface); |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 1297 | } |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1298 | |
Sunil Ravi | 3e88b0e | 2022-08-10 22:56:55 +0000 | [diff] [blame] | 1299 | err = hostapd_select_hw_mode(iface); |
| 1300 | if (err) { |
Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame] | 1301 | wpa_printf(MSG_ERROR, |
| 1302 | "ACS: Could not (err: %d) select hw_mode for freq=%d channel=%d", |
Sunil Ravi | 3e88b0e | 2022-08-10 22:56:55 +0000 | [diff] [blame] | 1303 | err, iface->freq, iface->conf->channel); |
| 1304 | err = -1; |
| 1305 | goto fail; |
| 1306 | } |
| 1307 | |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 1308 | err = 0; |
| 1309 | fail: |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1310 | /* |
| 1311 | * hostapd_setup_interface_complete() will return -1 on failure, |
| 1312 | * 0 on success and 0 is HOSTAPD_CHAN_VALID :) |
| 1313 | */ |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 1314 | if (hostapd_acs_completed(iface, err) == HOSTAPD_CHAN_VALID) { |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1315 | acs_cleanup(iface); |
| 1316 | return; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1317 | } |
| 1318 | |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 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."); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 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; |
Sunil Ravi | 7f76929 | 2024-07-23 22:21:32 +0000 | [diff] [blame] | 1331 | iface->acs_num_retries = 0; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 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"); |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 1339 | goto fail; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | if (++iface->acs_num_completed_scans < iface->conf->acs_num_scans) { |
| 1343 | err = acs_request_scan(iface); |
Sunil Ravi | 7f76929 | 2024-07-23 22:21:32 +0000 | [diff] [blame] | 1344 | if (err && err != -EBUSY) { |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1345 | wpa_printf(MSG_ERROR, "ACS: Failed to request scan"); |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 1346 | goto fail; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1347 | } |
| 1348 | |
| 1349 | return; |
| 1350 | } |
| 1351 | |
| 1352 | acs_study(iface); |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 1353 | return; |
| 1354 | fail: |
| 1355 | hostapd_acs_completed(iface, 1); |
| 1356 | acs_fail(iface); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1357 | } |
| 1358 | |
| 1359 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1360 | static int * acs_request_scan_add_freqs(struct hostapd_iface *iface, |
| 1361 | struct hostapd_hw_modes *mode, |
| 1362 | int *freq) |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1363 | { |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1364 | struct hostapd_channel_data *chan; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1365 | int i; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1366 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1367 | for (i = 0; i < mode->num_channels; i++) { |
| 1368 | chan = &mode->channels[i]; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1369 | if ((chan->flag & HOSTAPD_CHAN_DISABLED) || |
| 1370 | ((chan->flag & HOSTAPD_CHAN_RADAR) && |
| 1371 | iface->conf->acs_exclude_dfs)) |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1372 | continue; |
| 1373 | |
Dmitry Shmidt | 8bd70b7 | 2015-05-26 16:02:19 -0700 | [diff] [blame] | 1374 | if (!is_in_chanlist(iface, chan)) |
| 1375 | continue; |
| 1376 | |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame] | 1377 | if (!is_in_freqlist(iface, chan)) |
| 1378 | continue; |
| 1379 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 1380 | if (chan->max_tx_power < iface->conf->min_tx_power) |
| 1381 | continue; |
| 1382 | |
Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame] | 1383 | if ((chan->flag & HOSTAPD_CHAN_INDOOR_ONLY) && |
| 1384 | iface->conf->country[2] == 0x4f) |
| 1385 | continue; |
| 1386 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1387 | *freq++ = chan->freq; |
| 1388 | } |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 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; |
Sunil Ravi | 7f76929 | 2024-07-23 22:21:32 +0000 | [diff] [blame] | 1397 | int i, *freq, ret; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1398 | int num_channels; |
| 1399 | struct hostapd_hw_modes *mode; |
| 1400 | |
| 1401 | os_memset(¶ms, 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 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1422 | *freq = 0; |
| 1423 | |
Neo Jou | e288170 | 2019-10-17 11:32:28 +0800 | [diff] [blame] | 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 | |
Sunil Ravi | 7f76929 | 2024-07-23 22:21:32 +0000 | [diff] [blame] | 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); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1440 | |
Sunil Ravi | 7f76929 | 2024-07-23 22:21:32 +0000 | [diff] [blame] | 1441 | ret = hostapd_driver_scan(iface->bss[0], ¶ms); |
| 1442 | os_free(params.freqs); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1443 | |
Sunil Ravi | 7f76929 | 2024-07-23 22:21:32 +0000 | [diff] [blame] | 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) { |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1463 | wpa_printf(MSG_ERROR, "ACS: Failed to request initial scan"); |
| 1464 | acs_cleanup(iface); |
| 1465 | return -1; |
| 1466 | } |
| 1467 | |
Sunil Ravi | 7f76929 | 2024-07-23 22:21:32 +0000 | [diff] [blame] | 1468 | iface->scan_cb = acs_scan_complete; |
| 1469 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1470 | return 0; |
| 1471 | } |
| 1472 | |
| 1473 | |
Sunil Ravi | 7f76929 | 2024-07-23 22:21:32 +0000 | [diff] [blame] | 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 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1486 | enum hostapd_chan_status acs_init(struct hostapd_iface *iface) |
| 1487 | { |
Sunil Ravi | 2a14cf1 | 2023-11-21 00:54:38 +0000 | [diff] [blame] | 1488 | int err; |
| 1489 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1490 | wpa_printf(MSG_INFO, "ACS: Automatic channel selection started, this may take a bit"); |
| 1491 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1492 | if (iface->drv_flags & WPA_DRIVER_FLAGS_ACS_OFFLOAD) { |
| 1493 | wpa_printf(MSG_INFO, "ACS: Offloading to driver"); |
Sunil Ravi | 2a14cf1 | 2023-11-21 00:54:38 +0000 | [diff] [blame] | 1494 | |
| 1495 | err = hostapd_drv_do_acs(iface->bss[0]); |
| 1496 | if (err) { |
| 1497 | if (err == 1) |
| 1498 | return HOSTAPD_CHAN_INVALID_NO_IR; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1499 | return HOSTAPD_CHAN_INVALID; |
Sunil Ravi | 2a14cf1 | 2023-11-21 00:54:38 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1502 | return HOSTAPD_CHAN_ACS; |
| 1503 | } |
| 1504 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1505 | if (!iface->current_mode && |
| 1506 | iface->conf->hw_mode != HOSTAPD_MODE_IEEE80211ANY) |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 1507 | return HOSTAPD_CHAN_INVALID; |
| 1508 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1509 | acs_cleanup(iface); |
| 1510 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1511 | if (acs_request_scan(iface) < 0) |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1512 | return HOSTAPD_CHAN_INVALID; |
| 1513 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 1514 | hostapd_set_state(iface, HAPD_IFACE_ACS); |
| 1515 | wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, ACS_EVENT_STARTED); |
| 1516 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1517 | return HOSTAPD_CHAN_ACS; |
| 1518 | } |