13ff40c12SJohn Marino /*
23ff40c12SJohn Marino * ACS - Automatic Channel Selection module
33ff40c12SJohn Marino * Copyright (c) 2011, Atheros Communications
43ff40c12SJohn Marino * Copyright (c) 2013, Qualcomm Atheros, Inc.
53ff40c12SJohn Marino *
63ff40c12SJohn Marino * This software may be distributed under the terms of the BSD license.
73ff40c12SJohn Marino * See README for more details.
83ff40c12SJohn Marino */
93ff40c12SJohn Marino
103ff40c12SJohn Marino #include "utils/includes.h"
113ff40c12SJohn Marino #include <math.h>
123ff40c12SJohn Marino
133ff40c12SJohn Marino #include "utils/common.h"
143ff40c12SJohn Marino #include "utils/list.h"
153ff40c12SJohn Marino #include "common/ieee802_11_defs.h"
16*a1157835SDaniel Fojt #include "common/hw_features_common.h"
173ff40c12SJohn Marino #include "common/wpa_ctrl.h"
183ff40c12SJohn Marino #include "drivers/driver.h"
193ff40c12SJohn Marino #include "hostapd.h"
203ff40c12SJohn Marino #include "ap_drv_ops.h"
213ff40c12SJohn Marino #include "ap_config.h"
223ff40c12SJohn Marino #include "hw_features.h"
233ff40c12SJohn Marino #include "acs.h"
243ff40c12SJohn Marino
253ff40c12SJohn Marino /*
263ff40c12SJohn Marino * Automatic Channel Selection
273ff40c12SJohn Marino * ===========================
283ff40c12SJohn Marino *
293ff40c12SJohn Marino * More info at
303ff40c12SJohn Marino * ------------
313ff40c12SJohn Marino * http://wireless.kernel.org/en/users/Documentation/acs
323ff40c12SJohn Marino *
333ff40c12SJohn Marino * How to use
343ff40c12SJohn Marino * ----------
353ff40c12SJohn Marino * - make sure you have CONFIG_ACS=y in hostapd's .config
363ff40c12SJohn Marino * - use channel=0 or channel=acs to enable ACS
373ff40c12SJohn Marino *
383ff40c12SJohn Marino * How does it work
393ff40c12SJohn Marino * ----------------
403ff40c12SJohn Marino * 1. passive scans are used to collect survey data
413ff40c12SJohn Marino * (it is assumed that scan trigger collection of survey data in driver)
423ff40c12SJohn Marino * 2. interference factor is calculated for each channel
433ff40c12SJohn Marino * 3. ideal channel is picked depending on channel width by using adjacent
443ff40c12SJohn Marino * channel interference factors
453ff40c12SJohn Marino *
463ff40c12SJohn Marino * Known limitations
473ff40c12SJohn Marino * -----------------
483ff40c12SJohn Marino * - Current implementation depends heavily on the amount of time willing to
493ff40c12SJohn Marino * spend gathering survey data during hostapd startup. Short traffic bursts
503ff40c12SJohn Marino * may be missed and a suboptimal channel may be picked.
513ff40c12SJohn Marino * - Ideal channel may end up overlapping a channel with 40 MHz intolerant BSS
523ff40c12SJohn Marino *
533ff40c12SJohn Marino * Todo / Ideas
543ff40c12SJohn Marino * ------------
553ff40c12SJohn Marino * - implement other interference computation methods
563ff40c12SJohn Marino * - BSS/RSSI based
573ff40c12SJohn Marino * - spectral scan based
583ff40c12SJohn Marino * (should be possibly to hook this up with current ACS scans)
593ff40c12SJohn Marino * - add wpa_supplicant support (for P2P)
603ff40c12SJohn Marino * - collect a histogram of interference over time allowing more educated
613ff40c12SJohn Marino * guess about an ideal channel (perhaps CSA could be used to migrate AP to a
623ff40c12SJohn Marino * new "better" channel while running)
633ff40c12SJohn Marino * - include neighboring BSS scan to avoid conflicts with 40 MHz intolerant BSSs
643ff40c12SJohn Marino * when choosing the ideal channel
653ff40c12SJohn Marino *
663ff40c12SJohn Marino * Survey interference factor implementation details
673ff40c12SJohn Marino * -------------------------------------------------
683ff40c12SJohn Marino * Generic interference_factor in struct hostapd_channel_data is used.
693ff40c12SJohn Marino *
703ff40c12SJohn Marino * The survey interference factor is defined as the ratio of the
713ff40c12SJohn Marino * observed busy time over the time we spent on the channel,
723ff40c12SJohn Marino * this value is then amplified by the observed noise floor on
733ff40c12SJohn Marino * the channel in comparison to the lowest noise floor observed
743ff40c12SJohn Marino * on the entire band.
753ff40c12SJohn Marino *
763ff40c12SJohn Marino * This corresponds to:
773ff40c12SJohn Marino * ---
783ff40c12SJohn Marino * (busy time - tx time) / (active time - tx time) * 2^(chan_nf + band_min_nf)
793ff40c12SJohn Marino * ---
803ff40c12SJohn Marino *
813ff40c12SJohn Marino * The coefficient of 2 reflects the way power in "far-field"
823ff40c12SJohn Marino * radiation decreases as the square of distance from the antenna [1].
833ff40c12SJohn Marino * What this does is it decreases the observed busy time ratio if the
843ff40c12SJohn Marino * noise observed was low but increases it if the noise was high,
853ff40c12SJohn Marino * proportionally to the way "far field" radiation changes over
863ff40c12SJohn Marino * distance.
873ff40c12SJohn Marino *
883ff40c12SJohn Marino * If channel busy time is not available the fallback is to use channel RX time.
893ff40c12SJohn Marino *
903ff40c12SJohn Marino * Since noise floor is in dBm it is necessary to convert it into Watts so that
913ff40c12SJohn Marino * combined channel interference (e.g., HT40, which uses two channels) can be
923ff40c12SJohn Marino * calculated easily.
933ff40c12SJohn Marino * ---
943ff40c12SJohn Marino * (busy time - tx time) / (active time - tx time) *
953ff40c12SJohn Marino * 2^(10^(chan_nf/10) + 10^(band_min_nf/10))
963ff40c12SJohn Marino * ---
973ff40c12SJohn Marino *
983ff40c12SJohn Marino * However to account for cases where busy/rx time is 0 (channel load is then
993ff40c12SJohn Marino * 0%) channel noise floor signal power is combined into the equation so a
1003ff40c12SJohn Marino * channel with lower noise floor is preferred. The equation becomes:
1013ff40c12SJohn Marino * ---
1023ff40c12SJohn Marino * 10^(chan_nf/5) + (busy time - tx time) / (active time - tx time) *
1033ff40c12SJohn Marino * 2^(10^(chan_nf/10) + 10^(band_min_nf/10))
1043ff40c12SJohn Marino * ---
1053ff40c12SJohn Marino *
1063ff40c12SJohn Marino * All this "interference factor" is purely subjective and only time
1073ff40c12SJohn Marino * will tell how usable this is. By using the minimum noise floor we
1083ff40c12SJohn Marino * remove any possible issues due to card calibration. The computation
1093ff40c12SJohn Marino * of the interference factor then is dependent on what the card itself
1103ff40c12SJohn Marino * picks up as the minimum noise, not an actual real possible card
1113ff40c12SJohn Marino * noise value.
1123ff40c12SJohn Marino *
1133ff40c12SJohn Marino * Total interference computation details
1143ff40c12SJohn Marino * --------------------------------------
1153ff40c12SJohn Marino * The above channel interference factor is calculated with no respect to
1163ff40c12SJohn Marino * target operational bandwidth.
1173ff40c12SJohn Marino *
1183ff40c12SJohn Marino * To find an ideal channel the above data is combined by taking into account
1193ff40c12SJohn Marino * the target operational bandwidth and selected band. E.g., on 2.4 GHz channels
1203ff40c12SJohn Marino * overlap with 20 MHz bandwidth, but there is no overlap for 20 MHz bandwidth
1213ff40c12SJohn Marino * on 5 GHz.
1223ff40c12SJohn Marino *
1233ff40c12SJohn Marino * Each valid and possible channel spec (i.e., channel + width) is taken and its
1243ff40c12SJohn Marino * interference factor is computed by summing up interferences of each channel
1253ff40c12SJohn Marino * it overlaps. The one with least total interference is picked up.
1263ff40c12SJohn Marino *
1273ff40c12SJohn Marino * Note: This implies base channel interference factor must be non-negative
1283ff40c12SJohn Marino * allowing easy summing up.
1293ff40c12SJohn Marino *
1303ff40c12SJohn Marino * Example ACS analysis printout
1313ff40c12SJohn Marino * -----------------------------
1323ff40c12SJohn Marino *
1333ff40c12SJohn Marino * ACS: Trying survey-based ACS
1343ff40c12SJohn Marino * ACS: Survey analysis for channel 1 (2412 MHz)
1353ff40c12SJohn Marino * ACS: 1: min_nf=-113 interference_factor=0.0802469 nf=-113 time=162 busy=0 rx=13
1363ff40c12SJohn Marino * ACS: 2: min_nf=-113 interference_factor=0.0745342 nf=-113 time=161 busy=0 rx=12
1373ff40c12SJohn Marino * ACS: 3: min_nf=-113 interference_factor=0.0679012 nf=-113 time=162 busy=0 rx=11
1383ff40c12SJohn Marino * ACS: 4: min_nf=-113 interference_factor=0.0310559 nf=-113 time=161 busy=0 rx=5
1393ff40c12SJohn Marino * ACS: 5: min_nf=-113 interference_factor=0.0248447 nf=-113 time=161 busy=0 rx=4
1403ff40c12SJohn Marino * ACS: * interference factor average: 0.0557166
1413ff40c12SJohn Marino * ACS: Survey analysis for channel 2 (2417 MHz)
1423ff40c12SJohn Marino * ACS: 1: min_nf=-113 interference_factor=0.0185185 nf=-113 time=162 busy=0 rx=3
1433ff40c12SJohn Marino * ACS: 2: min_nf=-113 interference_factor=0.0246914 nf=-113 time=162 busy=0 rx=4
1443ff40c12SJohn Marino * ACS: 3: min_nf=-113 interference_factor=0.037037 nf=-113 time=162 busy=0 rx=6
1453ff40c12SJohn Marino * ACS: 4: min_nf=-113 interference_factor=0.149068 nf=-113 time=161 busy=0 rx=24
1463ff40c12SJohn Marino * ACS: 5: min_nf=-113 interference_factor=0.0248447 nf=-113 time=161 busy=0 rx=4
1473ff40c12SJohn Marino * ACS: * interference factor average: 0.050832
1483ff40c12SJohn Marino * ACS: Survey analysis for channel 3 (2422 MHz)
1493ff40c12SJohn Marino * ACS: 1: min_nf=-113 interference_factor=2.51189e-23 nf=-113 time=162 busy=0 rx=0
1503ff40c12SJohn Marino * ACS: 2: min_nf=-113 interference_factor=0.0185185 nf=-113 time=162 busy=0 rx=3
1513ff40c12SJohn Marino * ACS: 3: min_nf=-113 interference_factor=0.0186335 nf=-113 time=161 busy=0 rx=3
1523ff40c12SJohn Marino * ACS: 4: min_nf=-113 interference_factor=0.0186335 nf=-113 time=161 busy=0 rx=3
1533ff40c12SJohn Marino * ACS: 5: min_nf=-113 interference_factor=0.0186335 nf=-113 time=161 busy=0 rx=3
1543ff40c12SJohn Marino * ACS: * interference factor average: 0.0148838
1553ff40c12SJohn Marino * ACS: Survey analysis for channel 4 (2427 MHz)
1563ff40c12SJohn Marino * ACS: 1: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0
1573ff40c12SJohn Marino * ACS: 2: min_nf=-114 interference_factor=0.0555556 nf=-114 time=162 busy=0 rx=9
1583ff40c12SJohn Marino * ACS: 3: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=161 busy=0 rx=0
1593ff40c12SJohn Marino * ACS: 4: min_nf=-114 interference_factor=0.0186335 nf=-114 time=161 busy=0 rx=3
1603ff40c12SJohn Marino * ACS: 5: min_nf=-114 interference_factor=0.00621118 nf=-114 time=161 busy=0 rx=1
1613ff40c12SJohn Marino * ACS: * interference factor average: 0.0160801
1623ff40c12SJohn Marino * ACS: Survey analysis for channel 5 (2432 MHz)
1633ff40c12SJohn Marino * ACS: 1: min_nf=-114 interference_factor=0.409938 nf=-113 time=161 busy=0 rx=66
1643ff40c12SJohn Marino * ACS: 2: min_nf=-114 interference_factor=0.0432099 nf=-113 time=162 busy=0 rx=7
1653ff40c12SJohn Marino * ACS: 3: min_nf=-114 interference_factor=0.0124224 nf=-113 time=161 busy=0 rx=2
1663ff40c12SJohn Marino * ACS: 4: min_nf=-114 interference_factor=0.677019 nf=-113 time=161 busy=0 rx=109
1673ff40c12SJohn Marino * ACS: 5: min_nf=-114 interference_factor=0.0186335 nf=-114 time=161 busy=0 rx=3
1683ff40c12SJohn Marino * ACS: * interference factor average: 0.232244
1693ff40c12SJohn Marino * ACS: Survey analysis for channel 6 (2437 MHz)
1703ff40c12SJohn Marino * ACS: 1: min_nf=-113 interference_factor=0.552795 nf=-113 time=161 busy=0 rx=89
1713ff40c12SJohn Marino * ACS: 2: min_nf=-113 interference_factor=0.0807453 nf=-112 time=161 busy=0 rx=13
1723ff40c12SJohn Marino * ACS: 3: min_nf=-113 interference_factor=0.0310559 nf=-113 time=161 busy=0 rx=5
1733ff40c12SJohn Marino * ACS: 4: min_nf=-113 interference_factor=0.434783 nf=-112 time=161 busy=0 rx=70
1743ff40c12SJohn Marino * ACS: 5: min_nf=-113 interference_factor=0.0621118 nf=-113 time=161 busy=0 rx=10
1753ff40c12SJohn Marino * ACS: * interference factor average: 0.232298
1763ff40c12SJohn Marino * ACS: Survey analysis for channel 7 (2442 MHz)
1773ff40c12SJohn Marino * ACS: 1: min_nf=-113 interference_factor=0.440994 nf=-112 time=161 busy=0 rx=71
1783ff40c12SJohn Marino * ACS: 2: min_nf=-113 interference_factor=0.385093 nf=-113 time=161 busy=0 rx=62
1793ff40c12SJohn Marino * ACS: 3: min_nf=-113 interference_factor=0.0372671 nf=-113 time=161 busy=0 rx=6
1803ff40c12SJohn Marino * ACS: 4: min_nf=-113 interference_factor=0.0372671 nf=-113 time=161 busy=0 rx=6
1813ff40c12SJohn Marino * ACS: 5: min_nf=-113 interference_factor=0.0745342 nf=-113 time=161 busy=0 rx=12
1823ff40c12SJohn Marino * ACS: * interference factor average: 0.195031
1833ff40c12SJohn Marino * ACS: Survey analysis for channel 8 (2447 MHz)
1843ff40c12SJohn Marino * ACS: 1: min_nf=-114 interference_factor=0.0496894 nf=-112 time=161 busy=0 rx=8
1853ff40c12SJohn Marino * ACS: 2: min_nf=-114 interference_factor=0.0496894 nf=-114 time=161 busy=0 rx=8
1863ff40c12SJohn Marino * ACS: 3: min_nf=-114 interference_factor=0.0372671 nf=-113 time=161 busy=0 rx=6
1873ff40c12SJohn Marino * ACS: 4: min_nf=-114 interference_factor=0.12963 nf=-113 time=162 busy=0 rx=21
1883ff40c12SJohn Marino * ACS: 5: min_nf=-114 interference_factor=0.166667 nf=-114 time=162 busy=0 rx=27
1893ff40c12SJohn Marino * ACS: * interference factor average: 0.0865885
1903ff40c12SJohn Marino * ACS: Survey analysis for channel 9 (2452 MHz)
1913ff40c12SJohn Marino * ACS: 1: min_nf=-114 interference_factor=0.0124224 nf=-114 time=161 busy=0 rx=2
1923ff40c12SJohn Marino * ACS: 2: min_nf=-114 interference_factor=0.0310559 nf=-114 time=161 busy=0 rx=5
1933ff40c12SJohn Marino * ACS: 3: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=161 busy=0 rx=0
1943ff40c12SJohn Marino * ACS: 4: min_nf=-114 interference_factor=0.00617284 nf=-114 time=162 busy=0 rx=1
1953ff40c12SJohn Marino * ACS: 5: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0
1963ff40c12SJohn Marino * ACS: * interference factor average: 0.00993022
1973ff40c12SJohn Marino * ACS: Survey analysis for channel 10 (2457 MHz)
1983ff40c12SJohn Marino * ACS: 1: min_nf=-114 interference_factor=0.00621118 nf=-114 time=161 busy=0 rx=1
1993ff40c12SJohn Marino * ACS: 2: min_nf=-114 interference_factor=0.00621118 nf=-114 time=161 busy=0 rx=1
2003ff40c12SJohn Marino * ACS: 3: min_nf=-114 interference_factor=0.00621118 nf=-114 time=161 busy=0 rx=1
2013ff40c12SJohn Marino * ACS: 4: min_nf=-114 interference_factor=0.0493827 nf=-114 time=162 busy=0 rx=8
2023ff40c12SJohn Marino * ACS: 5: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0
2033ff40c12SJohn Marino * ACS: * interference factor average: 0.0136033
2043ff40c12SJohn Marino * ACS: Survey analysis for channel 11 (2462 MHz)
2053ff40c12SJohn Marino * ACS: 1: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=161 busy=0 rx=0
2063ff40c12SJohn Marino * ACS: 2: min_nf=-114 interference_factor=2.51189e-23 nf=-113 time=161 busy=0 rx=0
2073ff40c12SJohn Marino * ACS: 3: min_nf=-114 interference_factor=2.51189e-23 nf=-113 time=161 busy=0 rx=0
2083ff40c12SJohn Marino * ACS: 4: min_nf=-114 interference_factor=0.0432099 nf=-114 time=162 busy=0 rx=7
2093ff40c12SJohn Marino * ACS: 5: min_nf=-114 interference_factor=0.0925926 nf=-114 time=162 busy=0 rx=15
2103ff40c12SJohn Marino * ACS: * interference factor average: 0.0271605
2113ff40c12SJohn Marino * ACS: Survey analysis for channel 12 (2467 MHz)
2123ff40c12SJohn Marino * ACS: 1: min_nf=-114 interference_factor=0.0621118 nf=-113 time=161 busy=0 rx=10
2133ff40c12SJohn Marino * ACS: 2: min_nf=-114 interference_factor=0.00621118 nf=-114 time=161 busy=0 rx=1
2143ff40c12SJohn Marino * ACS: 3: min_nf=-114 interference_factor=2.51189e-23 nf=-113 time=162 busy=0 rx=0
2153ff40c12SJohn Marino * ACS: 4: min_nf=-114 interference_factor=2.51189e-23 nf=-113 time=162 busy=0 rx=0
2163ff40c12SJohn Marino * ACS: 5: min_nf=-114 interference_factor=0.00617284 nf=-113 time=162 busy=0 rx=1
2173ff40c12SJohn Marino * ACS: * interference factor average: 0.0148992
2183ff40c12SJohn Marino * ACS: Survey analysis for channel 13 (2472 MHz)
2193ff40c12SJohn Marino * ACS: 1: min_nf=-114 interference_factor=0.0745342 nf=-114 time=161 busy=0 rx=12
2203ff40c12SJohn Marino * ACS: 2: min_nf=-114 interference_factor=0.0555556 nf=-114 time=162 busy=0 rx=9
2213ff40c12SJohn Marino * ACS: 3: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0
2223ff40c12SJohn Marino * ACS: 4: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0
2233ff40c12SJohn Marino * ACS: 5: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0
2243ff40c12SJohn Marino * ACS: * interference factor average: 0.0260179
2253ff40c12SJohn Marino * ACS: Survey analysis for selected bandwidth 20MHz
2263ff40c12SJohn Marino * ACS: * channel 1: total interference = 0.121432
2273ff40c12SJohn Marino * ACS: * channel 2: total interference = 0.137512
2283ff40c12SJohn Marino * ACS: * channel 3: total interference = 0.369757
2293ff40c12SJohn Marino * ACS: * channel 4: total interference = 0.546338
2303ff40c12SJohn Marino * ACS: * channel 5: total interference = 0.690538
2313ff40c12SJohn Marino * ACS: * channel 6: total interference = 0.762242
2323ff40c12SJohn Marino * ACS: * channel 7: total interference = 0.756092
2333ff40c12SJohn Marino * ACS: * channel 8: total interference = 0.537451
2343ff40c12SJohn Marino * ACS: * channel 9: total interference = 0.332313
2353ff40c12SJohn Marino * ACS: * channel 10: total interference = 0.152182
2363ff40c12SJohn Marino * ACS: * channel 11: total interference = 0.0916111
2373ff40c12SJohn Marino * ACS: * channel 12: total interference = 0.0816809
2383ff40c12SJohn Marino * ACS: * channel 13: total interference = 0.0680776
2393ff40c12SJohn Marino * ACS: Ideal channel is 13 (2472 MHz) with total interference factor of 0.0680776
2403ff40c12SJohn Marino *
2413ff40c12SJohn Marino * [1] http://en.wikipedia.org/wiki/Near_and_far_field
2423ff40c12SJohn Marino */
2433ff40c12SJohn Marino
2443ff40c12SJohn Marino
2453ff40c12SJohn Marino static int acs_request_scan(struct hostapd_iface *iface);
246*a1157835SDaniel Fojt static int acs_survey_is_sufficient(struct freq_survey *survey);
2473ff40c12SJohn Marino
2483ff40c12SJohn Marino
acs_clean_chan_surveys(struct hostapd_channel_data * chan)2493ff40c12SJohn Marino static void acs_clean_chan_surveys(struct hostapd_channel_data *chan)
2503ff40c12SJohn Marino {
2513ff40c12SJohn Marino struct freq_survey *survey, *tmp;
2523ff40c12SJohn Marino
2533ff40c12SJohn Marino if (dl_list_empty(&chan->survey_list))
2543ff40c12SJohn Marino return;
2553ff40c12SJohn Marino
2563ff40c12SJohn Marino dl_list_for_each_safe(survey, tmp, &chan->survey_list,
2573ff40c12SJohn Marino struct freq_survey, list) {
2583ff40c12SJohn Marino dl_list_del(&survey->list);
2593ff40c12SJohn Marino os_free(survey);
2603ff40c12SJohn Marino }
2613ff40c12SJohn Marino }
2623ff40c12SJohn Marino
2633ff40c12SJohn Marino
acs_cleanup(struct hostapd_iface * iface)264*a1157835SDaniel Fojt void acs_cleanup(struct hostapd_iface *iface)
2653ff40c12SJohn Marino {
2663ff40c12SJohn Marino int i;
2673ff40c12SJohn Marino struct hostapd_channel_data *chan;
2683ff40c12SJohn Marino
2693ff40c12SJohn Marino for (i = 0; i < iface->current_mode->num_channels; i++) {
2703ff40c12SJohn Marino chan = &iface->current_mode->channels[i];
2713ff40c12SJohn Marino
2723ff40c12SJohn Marino if (chan->flag & HOSTAPD_CHAN_SURVEY_LIST_INITIALIZED)
2733ff40c12SJohn Marino acs_clean_chan_surveys(chan);
2743ff40c12SJohn Marino
2753ff40c12SJohn Marino dl_list_init(&chan->survey_list);
2763ff40c12SJohn Marino chan->flag |= HOSTAPD_CHAN_SURVEY_LIST_INITIALIZED;
2773ff40c12SJohn Marino chan->min_nf = 0;
2783ff40c12SJohn Marino }
2793ff40c12SJohn Marino
2803ff40c12SJohn Marino iface->chans_surveyed = 0;
2813ff40c12SJohn Marino iface->acs_num_completed_scans = 0;
2823ff40c12SJohn Marino }
2833ff40c12SJohn Marino
2843ff40c12SJohn Marino
acs_fail(struct hostapd_iface * iface)2853ff40c12SJohn Marino static void acs_fail(struct hostapd_iface *iface)
2863ff40c12SJohn Marino {
2873ff40c12SJohn Marino wpa_printf(MSG_ERROR, "ACS: Failed to start");
2883ff40c12SJohn Marino acs_cleanup(iface);
289*a1157835SDaniel Fojt hostapd_disable_iface(iface);
2903ff40c12SJohn Marino }
2913ff40c12SJohn Marino
2923ff40c12SJohn Marino
2933ff40c12SJohn Marino static long double
acs_survey_interference_factor(struct freq_survey * survey,s8 min_nf)2943ff40c12SJohn Marino acs_survey_interference_factor(struct freq_survey *survey, s8 min_nf)
2953ff40c12SJohn Marino {
2963ff40c12SJohn Marino long double factor, busy, total;
2973ff40c12SJohn Marino
2983ff40c12SJohn Marino if (survey->filled & SURVEY_HAS_CHAN_TIME_BUSY)
2993ff40c12SJohn Marino busy = survey->channel_time_busy;
3003ff40c12SJohn Marino else if (survey->filled & SURVEY_HAS_CHAN_TIME_RX)
3013ff40c12SJohn Marino busy = survey->channel_time_rx;
3023ff40c12SJohn Marino else {
3033ff40c12SJohn Marino /* This shouldn't really happen as survey data is checked in
3043ff40c12SJohn Marino * acs_sanity_check() */
3053ff40c12SJohn Marino wpa_printf(MSG_ERROR, "ACS: Survey data missing");
3063ff40c12SJohn Marino return 0;
3073ff40c12SJohn Marino }
3083ff40c12SJohn Marino
3093ff40c12SJohn Marino total = survey->channel_time;
3103ff40c12SJohn Marino
3113ff40c12SJohn Marino if (survey->filled & SURVEY_HAS_CHAN_TIME_TX) {
3123ff40c12SJohn Marino busy -= survey->channel_time_tx;
3133ff40c12SJohn Marino total -= survey->channel_time_tx;
3143ff40c12SJohn Marino }
3153ff40c12SJohn Marino
3163ff40c12SJohn Marino /* TODO: figure out the best multiplier for noise floor base */
3173ff40c12SJohn Marino factor = pow(10, survey->nf / 5.0L) +
318*a1157835SDaniel Fojt (total ? (busy / total) : 0) *
3193ff40c12SJohn Marino pow(2, pow(10, (long double) survey->nf / 10.0L) -
3203ff40c12SJohn Marino pow(10, (long double) min_nf / 10.0L));
3213ff40c12SJohn Marino
3223ff40c12SJohn Marino return factor;
3233ff40c12SJohn Marino }
3243ff40c12SJohn Marino
3253ff40c12SJohn Marino
3263ff40c12SJohn Marino static void
acs_survey_chan_interference_factor(struct hostapd_iface * iface,struct hostapd_channel_data * chan)3273ff40c12SJohn Marino acs_survey_chan_interference_factor(struct hostapd_iface *iface,
3283ff40c12SJohn Marino struct hostapd_channel_data *chan)
3293ff40c12SJohn Marino {
3303ff40c12SJohn Marino struct freq_survey *survey;
3313ff40c12SJohn Marino unsigned int i = 0;
3323ff40c12SJohn Marino long double int_factor = 0;
333*a1157835SDaniel Fojt unsigned count = 0;
3343ff40c12SJohn Marino
335*a1157835SDaniel Fojt if (dl_list_empty(&chan->survey_list) ||
336*a1157835SDaniel Fojt (chan->flag & HOSTAPD_CHAN_DISABLED))
3373ff40c12SJohn Marino return;
3383ff40c12SJohn Marino
3393ff40c12SJohn Marino chan->interference_factor = 0;
3403ff40c12SJohn Marino
3413ff40c12SJohn Marino dl_list_for_each(survey, &chan->survey_list, struct freq_survey, list)
3423ff40c12SJohn Marino {
343*a1157835SDaniel Fojt i++;
344*a1157835SDaniel Fojt
345*a1157835SDaniel Fojt if (!acs_survey_is_sufficient(survey)) {
346*a1157835SDaniel Fojt wpa_printf(MSG_DEBUG, "ACS: %d: insufficient data", i);
347*a1157835SDaniel Fojt continue;
348*a1157835SDaniel Fojt }
349*a1157835SDaniel Fojt
350*a1157835SDaniel Fojt count++;
3513ff40c12SJohn Marino int_factor = acs_survey_interference_factor(survey,
3523ff40c12SJohn Marino iface->lowest_nf);
3533ff40c12SJohn Marino chan->interference_factor += int_factor;
3543ff40c12SJohn Marino wpa_printf(MSG_DEBUG, "ACS: %d: min_nf=%d interference_factor=%Lg nf=%d time=%lu busy=%lu rx=%lu",
355*a1157835SDaniel Fojt i, chan->min_nf, int_factor,
3563ff40c12SJohn Marino survey->nf, (unsigned long) survey->channel_time,
3573ff40c12SJohn Marino (unsigned long) survey->channel_time_busy,
3583ff40c12SJohn Marino (unsigned long) survey->channel_time_rx);
3593ff40c12SJohn Marino }
3603ff40c12SJohn Marino
361*a1157835SDaniel Fojt if (count)
362*a1157835SDaniel Fojt chan->interference_factor /= count;
3633ff40c12SJohn Marino }
3643ff40c12SJohn Marino
3653ff40c12SJohn Marino
acs_usable_ht40_chan(const struct hostapd_channel_data * chan)366*a1157835SDaniel Fojt static int acs_usable_ht40_chan(const struct hostapd_channel_data *chan)
3673ff40c12SJohn Marino {
3683ff40c12SJohn Marino const int allowed[] = { 36, 44, 52, 60, 100, 108, 116, 124, 132, 149,
3693ff40c12SJohn Marino 157, 184, 192 };
3703ff40c12SJohn Marino unsigned int i;
3713ff40c12SJohn Marino
3723ff40c12SJohn Marino for (i = 0; i < ARRAY_SIZE(allowed); i++)
3733ff40c12SJohn Marino if (chan->chan == allowed[i])
3743ff40c12SJohn Marino return 1;
3753ff40c12SJohn Marino
3763ff40c12SJohn Marino return 0;
3773ff40c12SJohn Marino }
3783ff40c12SJohn Marino
3793ff40c12SJohn Marino
acs_usable_vht80_chan(const struct hostapd_channel_data * chan)380*a1157835SDaniel Fojt static int acs_usable_vht80_chan(const struct hostapd_channel_data *chan)
381*a1157835SDaniel Fojt {
382*a1157835SDaniel Fojt const int allowed[] = { 36, 52, 100, 116, 132, 149 };
383*a1157835SDaniel Fojt unsigned int i;
384*a1157835SDaniel Fojt
385*a1157835SDaniel Fojt for (i = 0; i < ARRAY_SIZE(allowed); i++)
386*a1157835SDaniel Fojt if (chan->chan == allowed[i])
387*a1157835SDaniel Fojt return 1;
388*a1157835SDaniel Fojt
389*a1157835SDaniel Fojt return 0;
390*a1157835SDaniel Fojt }
391*a1157835SDaniel Fojt
392*a1157835SDaniel Fojt
acs_usable_vht160_chan(const struct hostapd_channel_data * chan)393*a1157835SDaniel Fojt static int acs_usable_vht160_chan(const struct hostapd_channel_data *chan)
394*a1157835SDaniel Fojt {
395*a1157835SDaniel Fojt const int allowed[] = { 36, 100 };
396*a1157835SDaniel Fojt unsigned int i;
397*a1157835SDaniel Fojt
398*a1157835SDaniel Fojt for (i = 0; i < ARRAY_SIZE(allowed); i++)
399*a1157835SDaniel Fojt if (chan->chan == allowed[i])
400*a1157835SDaniel Fojt return 1;
401*a1157835SDaniel Fojt
402*a1157835SDaniel Fojt return 0;
403*a1157835SDaniel Fojt }
404*a1157835SDaniel Fojt
405*a1157835SDaniel Fojt
acs_survey_is_sufficient(struct freq_survey * survey)4063ff40c12SJohn Marino static int acs_survey_is_sufficient(struct freq_survey *survey)
4073ff40c12SJohn Marino {
4083ff40c12SJohn Marino if (!(survey->filled & SURVEY_HAS_NF)) {
409*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "ACS: Survey is missing noise floor");
4103ff40c12SJohn Marino return 0;
4113ff40c12SJohn Marino }
4123ff40c12SJohn Marino
4133ff40c12SJohn Marino if (!(survey->filled & SURVEY_HAS_CHAN_TIME)) {
414*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "ACS: Survey is missing channel time");
4153ff40c12SJohn Marino return 0;
4163ff40c12SJohn Marino }
4173ff40c12SJohn Marino
4183ff40c12SJohn Marino if (!(survey->filled & SURVEY_HAS_CHAN_TIME_BUSY) &&
4193ff40c12SJohn Marino !(survey->filled & SURVEY_HAS_CHAN_TIME_RX)) {
420*a1157835SDaniel Fojt wpa_printf(MSG_INFO,
421*a1157835SDaniel Fojt "ACS: Survey is missing RX and busy time (at least one is required)");
4223ff40c12SJohn Marino return 0;
4233ff40c12SJohn Marino }
4243ff40c12SJohn Marino
4253ff40c12SJohn Marino return 1;
4263ff40c12SJohn Marino }
4273ff40c12SJohn Marino
4283ff40c12SJohn Marino
acs_survey_list_is_sufficient(struct hostapd_channel_data * chan)4293ff40c12SJohn Marino static int acs_survey_list_is_sufficient(struct hostapd_channel_data *chan)
4303ff40c12SJohn Marino {
4313ff40c12SJohn Marino struct freq_survey *survey;
432*a1157835SDaniel Fojt int ret = -1;
4333ff40c12SJohn Marino
4343ff40c12SJohn Marino dl_list_for_each(survey, &chan->survey_list, struct freq_survey, list)
4353ff40c12SJohn Marino {
436*a1157835SDaniel Fojt if (acs_survey_is_sufficient(survey)) {
437*a1157835SDaniel Fojt ret = 1;
438*a1157835SDaniel Fojt break;
439*a1157835SDaniel Fojt }
440*a1157835SDaniel Fojt ret = 0;
441*a1157835SDaniel Fojt }
442*a1157835SDaniel Fojt
443*a1157835SDaniel Fojt if (ret == -1)
444*a1157835SDaniel Fojt ret = 1; /* no survey list entries */
445*a1157835SDaniel Fojt
446*a1157835SDaniel Fojt if (!ret) {
447*a1157835SDaniel Fojt wpa_printf(MSG_INFO,
448*a1157835SDaniel Fojt "ACS: Channel %d has insufficient survey data",
4493ff40c12SJohn Marino chan->chan);
4503ff40c12SJohn Marino }
4513ff40c12SJohn Marino
452*a1157835SDaniel Fojt return ret;
4533ff40c12SJohn Marino }
4543ff40c12SJohn Marino
4553ff40c12SJohn Marino
acs_surveys_are_sufficient(struct hostapd_iface * iface)4563ff40c12SJohn Marino static int acs_surveys_are_sufficient(struct hostapd_iface *iface)
4573ff40c12SJohn Marino {
4583ff40c12SJohn Marino int i;
4593ff40c12SJohn Marino struct hostapd_channel_data *chan;
4603ff40c12SJohn Marino int valid = 0;
4613ff40c12SJohn Marino
4623ff40c12SJohn Marino for (i = 0; i < iface->current_mode->num_channels; i++) {
4633ff40c12SJohn Marino chan = &iface->current_mode->channels[i];
464*a1157835SDaniel Fojt if (!(chan->flag & HOSTAPD_CHAN_DISABLED) &&
465*a1157835SDaniel Fojt acs_survey_list_is_sufficient(chan))
4663ff40c12SJohn Marino valid++;
4673ff40c12SJohn Marino }
4683ff40c12SJohn Marino
4693ff40c12SJohn Marino /* We need at least survey data for one channel */
4703ff40c12SJohn Marino return !!valid;
4713ff40c12SJohn Marino }
4723ff40c12SJohn Marino
4733ff40c12SJohn Marino
acs_usable_chan(struct hostapd_channel_data * chan)4743ff40c12SJohn Marino static int acs_usable_chan(struct hostapd_channel_data *chan)
4753ff40c12SJohn Marino {
476*a1157835SDaniel Fojt return !dl_list_empty(&chan->survey_list) &&
477*a1157835SDaniel Fojt !(chan->flag & HOSTAPD_CHAN_DISABLED) &&
478*a1157835SDaniel Fojt acs_survey_list_is_sufficient(chan);
479*a1157835SDaniel Fojt }
480*a1157835SDaniel Fojt
481*a1157835SDaniel Fojt
is_in_chanlist(struct hostapd_iface * iface,struct hostapd_channel_data * chan)482*a1157835SDaniel Fojt static int is_in_chanlist(struct hostapd_iface *iface,
483*a1157835SDaniel Fojt struct hostapd_channel_data *chan)
484*a1157835SDaniel Fojt {
485*a1157835SDaniel Fojt if (!iface->conf->acs_ch_list.num)
4863ff40c12SJohn Marino return 1;
487*a1157835SDaniel Fojt
488*a1157835SDaniel Fojt return freq_range_list_includes(&iface->conf->acs_ch_list, chan->chan);
4893ff40c12SJohn Marino }
4903ff40c12SJohn Marino
4913ff40c12SJohn Marino
acs_survey_all_chans_intereference_factor(struct hostapd_iface * iface)4923ff40c12SJohn Marino static void acs_survey_all_chans_intereference_factor(
4933ff40c12SJohn Marino struct hostapd_iface *iface)
4943ff40c12SJohn Marino {
4953ff40c12SJohn Marino int i;
4963ff40c12SJohn Marino struct hostapd_channel_data *chan;
4973ff40c12SJohn Marino
4983ff40c12SJohn Marino for (i = 0; i < iface->current_mode->num_channels; i++) {
4993ff40c12SJohn Marino chan = &iface->current_mode->channels[i];
5003ff40c12SJohn Marino
5013ff40c12SJohn Marino if (!acs_usable_chan(chan))
5023ff40c12SJohn Marino continue;
5033ff40c12SJohn Marino
504*a1157835SDaniel Fojt if (!is_in_chanlist(iface, chan))
505*a1157835SDaniel Fojt continue;
506*a1157835SDaniel Fojt
5073ff40c12SJohn Marino wpa_printf(MSG_DEBUG, "ACS: Survey analysis for channel %d (%d MHz)",
5083ff40c12SJohn Marino chan->chan, chan->freq);
5093ff40c12SJohn Marino
5103ff40c12SJohn Marino acs_survey_chan_interference_factor(iface, chan);
5113ff40c12SJohn Marino
5123ff40c12SJohn Marino wpa_printf(MSG_DEBUG, "ACS: * interference factor average: %Lg",
5133ff40c12SJohn Marino chan->interference_factor);
5143ff40c12SJohn Marino }
5153ff40c12SJohn Marino }
5163ff40c12SJohn Marino
5173ff40c12SJohn Marino
acs_find_chan(struct hostapd_iface * iface,int freq)5183ff40c12SJohn Marino static struct hostapd_channel_data *acs_find_chan(struct hostapd_iface *iface,
5193ff40c12SJohn Marino int freq)
5203ff40c12SJohn Marino {
5213ff40c12SJohn Marino struct hostapd_channel_data *chan;
5223ff40c12SJohn Marino int i;
5233ff40c12SJohn Marino
5243ff40c12SJohn Marino for (i = 0; i < iface->current_mode->num_channels; i++) {
5253ff40c12SJohn Marino chan = &iface->current_mode->channels[i];
5263ff40c12SJohn Marino
5273ff40c12SJohn Marino if (chan->flag & HOSTAPD_CHAN_DISABLED)
5283ff40c12SJohn Marino continue;
5293ff40c12SJohn Marino
5303ff40c12SJohn Marino if (chan->freq == freq)
5313ff40c12SJohn Marino return chan;
5323ff40c12SJohn Marino }
5333ff40c12SJohn Marino
5343ff40c12SJohn Marino return NULL;
5353ff40c12SJohn Marino }
5363ff40c12SJohn Marino
5373ff40c12SJohn Marino
is_24ghz_mode(enum hostapd_hw_mode mode)538*a1157835SDaniel Fojt static int is_24ghz_mode(enum hostapd_hw_mode mode)
539*a1157835SDaniel Fojt {
540*a1157835SDaniel Fojt return mode == HOSTAPD_MODE_IEEE80211B ||
541*a1157835SDaniel Fojt mode == HOSTAPD_MODE_IEEE80211G;
542*a1157835SDaniel Fojt }
543*a1157835SDaniel Fojt
544*a1157835SDaniel Fojt
is_common_24ghz_chan(int chan)545*a1157835SDaniel Fojt static int is_common_24ghz_chan(int chan)
546*a1157835SDaniel Fojt {
547*a1157835SDaniel Fojt return chan == 1 || chan == 6 || chan == 11;
548*a1157835SDaniel Fojt }
549*a1157835SDaniel Fojt
550*a1157835SDaniel Fojt
551*a1157835SDaniel Fojt #ifndef ACS_ADJ_WEIGHT
552*a1157835SDaniel Fojt #define ACS_ADJ_WEIGHT 0.85
553*a1157835SDaniel Fojt #endif /* ACS_ADJ_WEIGHT */
554*a1157835SDaniel Fojt
555*a1157835SDaniel Fojt #ifndef ACS_NEXT_ADJ_WEIGHT
556*a1157835SDaniel Fojt #define ACS_NEXT_ADJ_WEIGHT 0.55
557*a1157835SDaniel Fojt #endif /* ACS_NEXT_ADJ_WEIGHT */
558*a1157835SDaniel Fojt
559*a1157835SDaniel Fojt #ifndef ACS_24GHZ_PREFER_1_6_11
560*a1157835SDaniel Fojt /*
561*a1157835SDaniel Fojt * Select commonly used channels 1, 6, 11 by default even if a neighboring
562*a1157835SDaniel Fojt * channel has a smaller interference factor as long as it is not better by more
563*a1157835SDaniel Fojt * than this multiplier.
564*a1157835SDaniel Fojt */
565*a1157835SDaniel Fojt #define ACS_24GHZ_PREFER_1_6_11 0.8
566*a1157835SDaniel Fojt #endif /* ACS_24GHZ_PREFER_1_6_11 */
567*a1157835SDaniel Fojt
5683ff40c12SJohn Marino /*
5693ff40c12SJohn Marino * At this point it's assumed chan->interface_factor has been computed.
5703ff40c12SJohn Marino * This function should be reusable regardless of interference computation
5713ff40c12SJohn Marino * option (survey, BSS, spectral, ...). chan->interference factor must be
5723ff40c12SJohn Marino * summable (i.e., must be always greater than zero).
5733ff40c12SJohn Marino */
5743ff40c12SJohn Marino static struct hostapd_channel_data *
acs_find_ideal_chan(struct hostapd_iface * iface)5753ff40c12SJohn Marino acs_find_ideal_chan(struct hostapd_iface *iface)
5763ff40c12SJohn Marino {
5773ff40c12SJohn Marino struct hostapd_channel_data *chan, *adj_chan, *ideal_chan = NULL,
5783ff40c12SJohn Marino *rand_chan = NULL;
5793ff40c12SJohn Marino long double factor, ideal_factor = 0;
5803ff40c12SJohn Marino int i, j;
5813ff40c12SJohn Marino int n_chans = 1;
582*a1157835SDaniel Fojt u32 bw;
583*a1157835SDaniel Fojt unsigned int k;
5843ff40c12SJohn Marino
5853ff40c12SJohn Marino /* TODO: HT40- support */
5863ff40c12SJohn Marino
5873ff40c12SJohn Marino if (iface->conf->ieee80211n &&
5883ff40c12SJohn Marino iface->conf->secondary_channel == -1) {
5893ff40c12SJohn Marino wpa_printf(MSG_ERROR, "ACS: HT40- is not supported yet. Please try HT40+");
5903ff40c12SJohn Marino return NULL;
5913ff40c12SJohn Marino }
5923ff40c12SJohn Marino
5933ff40c12SJohn Marino if (iface->conf->ieee80211n &&
5943ff40c12SJohn Marino iface->conf->secondary_channel)
5953ff40c12SJohn Marino n_chans = 2;
5963ff40c12SJohn Marino
597*a1157835SDaniel Fojt if (iface->conf->ieee80211ac || iface->conf->ieee80211ax) {
598*a1157835SDaniel Fojt switch (hostapd_get_oper_chwidth(iface->conf)) {
599*a1157835SDaniel Fojt case CHANWIDTH_80MHZ:
6003ff40c12SJohn Marino n_chans = 4;
601*a1157835SDaniel Fojt break;
602*a1157835SDaniel Fojt case CHANWIDTH_160MHZ:
603*a1157835SDaniel Fojt n_chans = 8;
604*a1157835SDaniel Fojt break;
605*a1157835SDaniel Fojt }
606*a1157835SDaniel Fojt }
6073ff40c12SJohn Marino
608*a1157835SDaniel Fojt bw = num_chan_to_bw(n_chans);
6093ff40c12SJohn Marino
610*a1157835SDaniel Fojt /* TODO: VHT/HE80+80. Update acs_adjust_center_freq() too. */
611*a1157835SDaniel Fojt
612*a1157835SDaniel Fojt wpa_printf(MSG_DEBUG,
613*a1157835SDaniel Fojt "ACS: Survey analysis for selected bandwidth %d MHz", bw);
6143ff40c12SJohn Marino
6153ff40c12SJohn Marino for (i = 0; i < iface->current_mode->num_channels; i++) {
616*a1157835SDaniel Fojt double total_weight;
617*a1157835SDaniel Fojt struct acs_bias *bias, tmp_bias;
618*a1157835SDaniel Fojt
6193ff40c12SJohn Marino chan = &iface->current_mode->channels[i];
6203ff40c12SJohn Marino
621*a1157835SDaniel Fojt /* Since in the current ACS implementation the first channel is
622*a1157835SDaniel Fojt * always a primary channel, skip channels not available as
623*a1157835SDaniel Fojt * primary until more sophisticated channel selection is
624*a1157835SDaniel Fojt * implemented. */
625*a1157835SDaniel Fojt if (!chan_pri_allowed(chan))
6263ff40c12SJohn Marino continue;
6273ff40c12SJohn Marino
628*a1157835SDaniel Fojt if (!is_in_chanlist(iface, chan))
629*a1157835SDaniel Fojt continue;
630*a1157835SDaniel Fojt
631*a1157835SDaniel Fojt if (!chan_bw_allowed(chan, bw, 1, 1)) {
632*a1157835SDaniel Fojt wpa_printf(MSG_DEBUG,
633*a1157835SDaniel Fojt "ACS: Channel %d: BW %u is not supported",
634*a1157835SDaniel Fojt chan->chan, bw);
635*a1157835SDaniel Fojt continue;
636*a1157835SDaniel Fojt }
6373ff40c12SJohn Marino
6383ff40c12SJohn Marino /* HT40 on 5 GHz has a limited set of primary channels as per
6393ff40c12SJohn Marino * 11n Annex J */
6403ff40c12SJohn Marino if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A &&
6413ff40c12SJohn Marino iface->conf->ieee80211n &&
6423ff40c12SJohn Marino iface->conf->secondary_channel &&
6433ff40c12SJohn Marino !acs_usable_ht40_chan(chan)) {
6443ff40c12SJohn Marino wpa_printf(MSG_DEBUG, "ACS: Channel %d: not allowed as primary channel for HT40",
6453ff40c12SJohn Marino chan->chan);
6463ff40c12SJohn Marino continue;
6473ff40c12SJohn Marino }
6483ff40c12SJohn Marino
649*a1157835SDaniel Fojt if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A &&
650*a1157835SDaniel Fojt (iface->conf->ieee80211ac || iface->conf->ieee80211ax)) {
651*a1157835SDaniel Fojt if (hostapd_get_oper_chwidth(iface->conf) ==
652*a1157835SDaniel Fojt CHANWIDTH_80MHZ &&
653*a1157835SDaniel Fojt !acs_usable_vht80_chan(chan)) {
654*a1157835SDaniel Fojt wpa_printf(MSG_DEBUG,
655*a1157835SDaniel Fojt "ACS: Channel %d: not allowed as primary channel for VHT80",
656*a1157835SDaniel Fojt chan->chan);
657*a1157835SDaniel Fojt continue;
658*a1157835SDaniel Fojt }
659*a1157835SDaniel Fojt
660*a1157835SDaniel Fojt if (hostapd_get_oper_chwidth(iface->conf) ==
661*a1157835SDaniel Fojt CHANWIDTH_160MHZ &&
662*a1157835SDaniel Fojt !acs_usable_vht160_chan(chan)) {
663*a1157835SDaniel Fojt wpa_printf(MSG_DEBUG,
664*a1157835SDaniel Fojt "ACS: Channel %d: not allowed as primary channel for VHT160",
665*a1157835SDaniel Fojt chan->chan);
666*a1157835SDaniel Fojt continue;
667*a1157835SDaniel Fojt }
668*a1157835SDaniel Fojt }
669*a1157835SDaniel Fojt
6703ff40c12SJohn Marino factor = 0;
6713ff40c12SJohn Marino if (acs_usable_chan(chan))
6723ff40c12SJohn Marino factor = chan->interference_factor;
673*a1157835SDaniel Fojt total_weight = 1;
6743ff40c12SJohn Marino
6753ff40c12SJohn Marino for (j = 1; j < n_chans; j++) {
6763ff40c12SJohn Marino adj_chan = acs_find_chan(iface, chan->freq + (j * 20));
6773ff40c12SJohn Marino if (!adj_chan)
6783ff40c12SJohn Marino break;
6793ff40c12SJohn Marino
680*a1157835SDaniel Fojt if (!chan_bw_allowed(adj_chan, bw, 1, 0)) {
681*a1157835SDaniel Fojt wpa_printf(MSG_DEBUG,
682*a1157835SDaniel Fojt "ACS: PRI Channel %d: secondary channel %d BW %u is not supported",
683*a1157835SDaniel Fojt chan->chan, adj_chan->chan, bw);
684*a1157835SDaniel Fojt break;
685*a1157835SDaniel Fojt }
686*a1157835SDaniel Fojt
687*a1157835SDaniel Fojt if (acs_usable_chan(adj_chan)) {
6883ff40c12SJohn Marino factor += adj_chan->interference_factor;
689*a1157835SDaniel Fojt total_weight += 1;
690*a1157835SDaniel Fojt }
6913ff40c12SJohn Marino }
6923ff40c12SJohn Marino
6933ff40c12SJohn Marino if (j != n_chans) {
6943ff40c12SJohn Marino wpa_printf(MSG_DEBUG, "ACS: Channel %d: not enough bandwidth",
6953ff40c12SJohn Marino chan->chan);
6963ff40c12SJohn Marino continue;
6973ff40c12SJohn Marino }
6983ff40c12SJohn Marino
6993ff40c12SJohn Marino /* 2.4 GHz has overlapping 20 MHz channels. Include adjacent
7003ff40c12SJohn Marino * channel interference factor. */
701*a1157835SDaniel Fojt if (is_24ghz_mode(iface->current_mode->mode)) {
7023ff40c12SJohn Marino for (j = 0; j < n_chans; j++) {
7033ff40c12SJohn Marino adj_chan = acs_find_chan(iface, chan->freq +
7043ff40c12SJohn Marino (j * 20) - 5);
705*a1157835SDaniel Fojt if (adj_chan && acs_usable_chan(adj_chan)) {
706*a1157835SDaniel Fojt factor += ACS_ADJ_WEIGHT *
707*a1157835SDaniel Fojt adj_chan->interference_factor;
708*a1157835SDaniel Fojt total_weight += ACS_ADJ_WEIGHT;
709*a1157835SDaniel Fojt }
7103ff40c12SJohn Marino
7113ff40c12SJohn Marino adj_chan = acs_find_chan(iface, chan->freq +
7123ff40c12SJohn Marino (j * 20) - 10);
713*a1157835SDaniel Fojt if (adj_chan && acs_usable_chan(adj_chan)) {
714*a1157835SDaniel Fojt factor += ACS_NEXT_ADJ_WEIGHT *
715*a1157835SDaniel Fojt adj_chan->interference_factor;
716*a1157835SDaniel Fojt total_weight += ACS_NEXT_ADJ_WEIGHT;
717*a1157835SDaniel Fojt }
7183ff40c12SJohn Marino
7193ff40c12SJohn Marino adj_chan = acs_find_chan(iface, chan->freq +
7203ff40c12SJohn Marino (j * 20) + 5);
721*a1157835SDaniel Fojt if (adj_chan && acs_usable_chan(adj_chan)) {
722*a1157835SDaniel Fojt factor += ACS_ADJ_WEIGHT *
723*a1157835SDaniel Fojt adj_chan->interference_factor;
724*a1157835SDaniel Fojt total_weight += ACS_ADJ_WEIGHT;
725*a1157835SDaniel Fojt }
7263ff40c12SJohn Marino
7273ff40c12SJohn Marino adj_chan = acs_find_chan(iface, chan->freq +
7283ff40c12SJohn Marino (j * 20) + 10);
729*a1157835SDaniel Fojt if (adj_chan && acs_usable_chan(adj_chan)) {
730*a1157835SDaniel Fojt factor += ACS_NEXT_ADJ_WEIGHT *
731*a1157835SDaniel Fojt adj_chan->interference_factor;
732*a1157835SDaniel Fojt total_weight += ACS_NEXT_ADJ_WEIGHT;
733*a1157835SDaniel Fojt }
7343ff40c12SJohn Marino }
7353ff40c12SJohn Marino }
7363ff40c12SJohn Marino
737*a1157835SDaniel Fojt factor /= total_weight;
738*a1157835SDaniel Fojt
739*a1157835SDaniel Fojt bias = NULL;
740*a1157835SDaniel Fojt if (iface->conf->acs_chan_bias) {
741*a1157835SDaniel Fojt for (k = 0; k < iface->conf->num_acs_chan_bias; k++) {
742*a1157835SDaniel Fojt bias = &iface->conf->acs_chan_bias[k];
743*a1157835SDaniel Fojt if (bias->channel == chan->chan)
744*a1157835SDaniel Fojt break;
745*a1157835SDaniel Fojt bias = NULL;
746*a1157835SDaniel Fojt }
747*a1157835SDaniel Fojt } else if (is_24ghz_mode(iface->current_mode->mode) &&
748*a1157835SDaniel Fojt is_common_24ghz_chan(chan->chan)) {
749*a1157835SDaniel Fojt tmp_bias.channel = chan->chan;
750*a1157835SDaniel Fojt tmp_bias.bias = ACS_24GHZ_PREFER_1_6_11;
751*a1157835SDaniel Fojt bias = &tmp_bias;
752*a1157835SDaniel Fojt }
753*a1157835SDaniel Fojt
754*a1157835SDaniel Fojt if (bias) {
755*a1157835SDaniel Fojt factor *= bias->bias;
756*a1157835SDaniel Fojt wpa_printf(MSG_DEBUG,
757*a1157835SDaniel Fojt "ACS: * channel %d: total interference = %Lg (%f bias)",
758*a1157835SDaniel Fojt chan->chan, factor, bias->bias);
759*a1157835SDaniel Fojt } else {
760*a1157835SDaniel Fojt wpa_printf(MSG_DEBUG,
761*a1157835SDaniel Fojt "ACS: * channel %d: total interference = %Lg",
7623ff40c12SJohn Marino chan->chan, factor);
763*a1157835SDaniel Fojt }
7643ff40c12SJohn Marino
7653ff40c12SJohn Marino if (acs_usable_chan(chan) &&
7663ff40c12SJohn Marino (!ideal_chan || factor < ideal_factor)) {
7673ff40c12SJohn Marino ideal_factor = factor;
7683ff40c12SJohn Marino ideal_chan = chan;
7693ff40c12SJohn Marino }
7703ff40c12SJohn Marino
7713ff40c12SJohn Marino /* This channel would at least be usable */
7723ff40c12SJohn Marino if (!rand_chan)
7733ff40c12SJohn Marino rand_chan = chan;
7743ff40c12SJohn Marino }
7753ff40c12SJohn Marino
7763ff40c12SJohn Marino if (ideal_chan) {
7773ff40c12SJohn Marino wpa_printf(MSG_DEBUG, "ACS: Ideal channel is %d (%d MHz) with total interference factor of %Lg",
7783ff40c12SJohn Marino ideal_chan->chan, ideal_chan->freq, ideal_factor);
7793ff40c12SJohn Marino return ideal_chan;
7803ff40c12SJohn Marino }
7813ff40c12SJohn Marino
7823ff40c12SJohn Marino return rand_chan;
7833ff40c12SJohn Marino }
7843ff40c12SJohn Marino
7853ff40c12SJohn Marino
acs_adjust_center_freq(struct hostapd_iface * iface)786*a1157835SDaniel Fojt static void acs_adjust_center_freq(struct hostapd_iface *iface)
7873ff40c12SJohn Marino {
788*a1157835SDaniel Fojt int offset;
789*a1157835SDaniel Fojt
7903ff40c12SJohn Marino wpa_printf(MSG_DEBUG, "ACS: Adjusting VHT center frequency");
7913ff40c12SJohn Marino
792*a1157835SDaniel Fojt switch (hostapd_get_oper_chwidth(iface->conf)) {
793*a1157835SDaniel Fojt case CHANWIDTH_USE_HT:
794*a1157835SDaniel Fojt offset = 2 * iface->conf->secondary_channel;
7953ff40c12SJohn Marino break;
796*a1157835SDaniel Fojt case CHANWIDTH_80MHZ:
797*a1157835SDaniel Fojt offset = 6;
798*a1157835SDaniel Fojt break;
799*a1157835SDaniel Fojt case CHANWIDTH_160MHZ:
800*a1157835SDaniel Fojt offset = 14;
8013ff40c12SJohn Marino break;
8023ff40c12SJohn Marino default:
8033ff40c12SJohn Marino /* TODO: How can this be calculated? Adjust
8043ff40c12SJohn Marino * acs_find_ideal_chan() */
805*a1157835SDaniel Fojt wpa_printf(MSG_INFO,
806*a1157835SDaniel Fojt "ACS: Only VHT20/40/80/160 is supported now");
807*a1157835SDaniel Fojt return;
8083ff40c12SJohn Marino }
809*a1157835SDaniel Fojt
810*a1157835SDaniel Fojt hostapd_set_oper_centr_freq_seg0_idx(iface->conf,
811*a1157835SDaniel Fojt iface->conf->channel + offset);
8123ff40c12SJohn Marino }
8133ff40c12SJohn Marino
8143ff40c12SJohn Marino
acs_study_survey_based(struct hostapd_iface * iface)8153ff40c12SJohn Marino static int acs_study_survey_based(struct hostapd_iface *iface)
8163ff40c12SJohn Marino {
8173ff40c12SJohn Marino wpa_printf(MSG_DEBUG, "ACS: Trying survey-based ACS");
8183ff40c12SJohn Marino
8193ff40c12SJohn Marino if (!iface->chans_surveyed) {
8203ff40c12SJohn Marino wpa_printf(MSG_ERROR, "ACS: Unable to collect survey data");
8213ff40c12SJohn Marino return -1;
8223ff40c12SJohn Marino }
8233ff40c12SJohn Marino
8243ff40c12SJohn Marino if (!acs_surveys_are_sufficient(iface)) {
8253ff40c12SJohn Marino wpa_printf(MSG_ERROR, "ACS: Surveys have insufficient data");
8263ff40c12SJohn Marino return -1;
8273ff40c12SJohn Marino }
8283ff40c12SJohn Marino
8293ff40c12SJohn Marino acs_survey_all_chans_intereference_factor(iface);
8303ff40c12SJohn Marino return 0;
8313ff40c12SJohn Marino }
8323ff40c12SJohn Marino
8333ff40c12SJohn Marino
acs_study_options(struct hostapd_iface * iface)8343ff40c12SJohn Marino static int acs_study_options(struct hostapd_iface *iface)
8353ff40c12SJohn Marino {
836*a1157835SDaniel Fojt if (acs_study_survey_based(iface) == 0)
8373ff40c12SJohn Marino return 0;
8383ff40c12SJohn Marino
8393ff40c12SJohn Marino /* TODO: If no surveys are available/sufficient this is a good
8403ff40c12SJohn Marino * place to fallback to BSS-based ACS */
8413ff40c12SJohn Marino
8423ff40c12SJohn Marino return -1;
8433ff40c12SJohn Marino }
8443ff40c12SJohn Marino
8453ff40c12SJohn Marino
acs_study(struct hostapd_iface * iface)8463ff40c12SJohn Marino static void acs_study(struct hostapd_iface *iface)
8473ff40c12SJohn Marino {
8483ff40c12SJohn Marino struct hostapd_channel_data *ideal_chan;
8493ff40c12SJohn Marino int err;
8503ff40c12SJohn Marino
8513ff40c12SJohn Marino err = acs_study_options(iface);
8523ff40c12SJohn Marino if (err < 0) {
8533ff40c12SJohn Marino wpa_printf(MSG_ERROR, "ACS: All study options have failed");
8543ff40c12SJohn Marino goto fail;
8553ff40c12SJohn Marino }
8563ff40c12SJohn Marino
8573ff40c12SJohn Marino ideal_chan = acs_find_ideal_chan(iface);
8583ff40c12SJohn Marino if (!ideal_chan) {
8593ff40c12SJohn Marino wpa_printf(MSG_ERROR, "ACS: Failed to compute ideal channel");
8603ff40c12SJohn Marino err = -1;
8613ff40c12SJohn Marino goto fail;
8623ff40c12SJohn Marino }
8633ff40c12SJohn Marino
8643ff40c12SJohn Marino iface->conf->channel = ideal_chan->chan;
8653ff40c12SJohn Marino
866*a1157835SDaniel Fojt if (iface->conf->ieee80211ac || iface->conf->ieee80211ax)
867*a1157835SDaniel Fojt acs_adjust_center_freq(iface);
8683ff40c12SJohn Marino
8693ff40c12SJohn Marino err = 0;
8703ff40c12SJohn Marino fail:
8713ff40c12SJohn Marino /*
8723ff40c12SJohn Marino * hostapd_setup_interface_complete() will return -1 on failure,
8733ff40c12SJohn Marino * 0 on success and 0 is HOSTAPD_CHAN_VALID :)
8743ff40c12SJohn Marino */
8753ff40c12SJohn Marino if (hostapd_acs_completed(iface, err) == HOSTAPD_CHAN_VALID) {
8763ff40c12SJohn Marino acs_cleanup(iface);
8773ff40c12SJohn Marino return;
8783ff40c12SJohn Marino }
8793ff40c12SJohn Marino
8803ff40c12SJohn Marino /* This can possibly happen if channel parameters (secondary
8813ff40c12SJohn Marino * channel, center frequencies) are misconfigured */
8823ff40c12SJohn Marino wpa_printf(MSG_ERROR, "ACS: Possibly channel configuration is invalid, please report this along with your config file.");
8833ff40c12SJohn Marino acs_fail(iface);
8843ff40c12SJohn Marino }
8853ff40c12SJohn Marino
8863ff40c12SJohn Marino
acs_scan_complete(struct hostapd_iface * iface)8873ff40c12SJohn Marino static void acs_scan_complete(struct hostapd_iface *iface)
8883ff40c12SJohn Marino {
8893ff40c12SJohn Marino int err;
8903ff40c12SJohn Marino
8913ff40c12SJohn Marino iface->scan_cb = NULL;
8923ff40c12SJohn Marino
8933ff40c12SJohn Marino wpa_printf(MSG_DEBUG, "ACS: Using survey based algorithm (acs_num_scans=%d)",
8943ff40c12SJohn Marino iface->conf->acs_num_scans);
8953ff40c12SJohn Marino
8963ff40c12SJohn Marino err = hostapd_drv_get_survey(iface->bss[0], 0);
8973ff40c12SJohn Marino if (err) {
8983ff40c12SJohn Marino wpa_printf(MSG_ERROR, "ACS: Failed to get survey data");
899*a1157835SDaniel Fojt goto fail;
9003ff40c12SJohn Marino }
9013ff40c12SJohn Marino
9023ff40c12SJohn Marino if (++iface->acs_num_completed_scans < iface->conf->acs_num_scans) {
9033ff40c12SJohn Marino err = acs_request_scan(iface);
9043ff40c12SJohn Marino if (err) {
9053ff40c12SJohn Marino wpa_printf(MSG_ERROR, "ACS: Failed to request scan");
9063ff40c12SJohn Marino goto fail;
9073ff40c12SJohn Marino }
9083ff40c12SJohn Marino
9093ff40c12SJohn Marino return;
9103ff40c12SJohn Marino }
9113ff40c12SJohn Marino
9123ff40c12SJohn Marino acs_study(iface);
9133ff40c12SJohn Marino return;
9143ff40c12SJohn Marino fail:
9153ff40c12SJohn Marino hostapd_acs_completed(iface, 1);
9163ff40c12SJohn Marino acs_fail(iface);
9173ff40c12SJohn Marino }
9183ff40c12SJohn Marino
9193ff40c12SJohn Marino
acs_request_scan(struct hostapd_iface * iface)9203ff40c12SJohn Marino static int acs_request_scan(struct hostapd_iface *iface)
9213ff40c12SJohn Marino {
9223ff40c12SJohn Marino struct wpa_driver_scan_params params;
9233ff40c12SJohn Marino struct hostapd_channel_data *chan;
9243ff40c12SJohn Marino int i, *freq;
9253ff40c12SJohn Marino
9263ff40c12SJohn Marino os_memset(¶ms, 0, sizeof(params));
9273ff40c12SJohn Marino params.freqs = os_calloc(iface->current_mode->num_channels + 1,
9283ff40c12SJohn Marino sizeof(params.freqs[0]));
9293ff40c12SJohn Marino if (params.freqs == NULL)
9303ff40c12SJohn Marino return -1;
9313ff40c12SJohn Marino
9323ff40c12SJohn Marino freq = params.freqs;
9333ff40c12SJohn Marino for (i = 0; i < iface->current_mode->num_channels; i++) {
9343ff40c12SJohn Marino chan = &iface->current_mode->channels[i];
9353ff40c12SJohn Marino if (chan->flag & HOSTAPD_CHAN_DISABLED)
9363ff40c12SJohn Marino continue;
9373ff40c12SJohn Marino
938*a1157835SDaniel Fojt if (!is_in_chanlist(iface, chan))
939*a1157835SDaniel Fojt continue;
940*a1157835SDaniel Fojt
9413ff40c12SJohn Marino *freq++ = chan->freq;
9423ff40c12SJohn Marino }
9433ff40c12SJohn Marino *freq = 0;
9443ff40c12SJohn Marino
9453ff40c12SJohn Marino iface->scan_cb = acs_scan_complete;
9463ff40c12SJohn Marino
9473ff40c12SJohn Marino wpa_printf(MSG_DEBUG, "ACS: Scanning %d / %d",
9483ff40c12SJohn Marino iface->acs_num_completed_scans + 1,
9493ff40c12SJohn Marino iface->conf->acs_num_scans);
9503ff40c12SJohn Marino
9513ff40c12SJohn Marino if (hostapd_driver_scan(iface->bss[0], ¶ms) < 0) {
9523ff40c12SJohn Marino wpa_printf(MSG_ERROR, "ACS: Failed to request initial scan");
9533ff40c12SJohn Marino acs_cleanup(iface);
954*a1157835SDaniel Fojt os_free(params.freqs);
9553ff40c12SJohn Marino return -1;
9563ff40c12SJohn Marino }
9573ff40c12SJohn Marino
9583ff40c12SJohn Marino os_free(params.freqs);
9593ff40c12SJohn Marino return 0;
9603ff40c12SJohn Marino }
9613ff40c12SJohn Marino
9623ff40c12SJohn Marino
acs_init(struct hostapd_iface * iface)9633ff40c12SJohn Marino enum hostapd_chan_status acs_init(struct hostapd_iface *iface)
9643ff40c12SJohn Marino {
9653ff40c12SJohn Marino wpa_printf(MSG_INFO, "ACS: Automatic channel selection started, this may take a bit");
9663ff40c12SJohn Marino
967*a1157835SDaniel Fojt if (iface->drv_flags & WPA_DRIVER_FLAGS_ACS_OFFLOAD) {
968*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "ACS: Offloading to driver");
969*a1157835SDaniel Fojt if (hostapd_drv_do_acs(iface->bss[0]))
970*a1157835SDaniel Fojt return HOSTAPD_CHAN_INVALID;
971*a1157835SDaniel Fojt return HOSTAPD_CHAN_ACS;
972*a1157835SDaniel Fojt }
973*a1157835SDaniel Fojt
974*a1157835SDaniel Fojt if (!iface->current_mode)
975*a1157835SDaniel Fojt return HOSTAPD_CHAN_INVALID;
976*a1157835SDaniel Fojt
9773ff40c12SJohn Marino acs_cleanup(iface);
9783ff40c12SJohn Marino
979*a1157835SDaniel Fojt if (acs_request_scan(iface) < 0)
9803ff40c12SJohn Marino return HOSTAPD_CHAN_INVALID;
9813ff40c12SJohn Marino
9823ff40c12SJohn Marino hostapd_set_state(iface, HAPD_IFACE_ACS);
9833ff40c12SJohn Marino wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, ACS_EVENT_STARTED);
9843ff40c12SJohn Marino
9853ff40c12SJohn Marino return HOSTAPD_CHAN_ACS;
9863ff40c12SJohn Marino }
987