acs.c 28 KB

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