bgscan_learn.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*
  2. * WPA Supplicant - background scan and roaming module: learn
  3. * Copyright (c) 2009-2010, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "eloop.h"
  17. #include "list.h"
  18. #include "common/ieee802_11_defs.h"
  19. #include "drivers/driver.h"
  20. #include "config_ssid.h"
  21. #include "wpa_supplicant_i.h"
  22. #include "driver_i.h"
  23. #include "scan.h"
  24. #include "bgscan.h"
  25. struct bgscan_learn_bss {
  26. struct dl_list list;
  27. u8 bssid[ETH_ALEN];
  28. int freq;
  29. };
  30. struct bgscan_learn_data {
  31. struct wpa_supplicant *wpa_s;
  32. const struct wpa_ssid *ssid;
  33. int scan_interval;
  34. int signal_threshold;
  35. int short_interval; /* use if signal < threshold */
  36. int long_interval; /* use if signal > threshold */
  37. struct os_time last_bgscan;
  38. char *fname;
  39. struct dl_list bss;
  40. int *supp_freqs;
  41. int probe_idx;
  42. };
  43. static struct bgscan_learn_bss * bgscan_learn_get_bss(
  44. struct bgscan_learn_data *data, const u8 *bssid)
  45. {
  46. struct bgscan_learn_bss *bss;
  47. dl_list_for_each(bss, &data->bss, struct bgscan_learn_bss, list) {
  48. if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
  49. return bss;
  50. }
  51. return NULL;
  52. }
  53. static int bgscan_learn_load(struct bgscan_learn_data *data)
  54. {
  55. FILE *f;
  56. char buf[128];
  57. if (data->fname == NULL)
  58. return 0;
  59. f = fopen(data->fname, "r");
  60. if (f == NULL)
  61. return 0;
  62. wpa_printf(MSG_DEBUG, "bgscan learn: Loading data from %s",
  63. data->fname);
  64. if (fgets(buf, sizeof(buf), f) == NULL ||
  65. os_strncmp(buf, "wpa_supplicant-bgscan-learn\n", 28) != 0) {
  66. wpa_printf(MSG_INFO, "bgscan learn: Invalid data file %s",
  67. data->fname);
  68. fclose(f);
  69. return -1;
  70. }
  71. while (fgets(buf, sizeof(buf), f)) {
  72. if (os_strncmp(buf, "BSS ", 4) == 0) {
  73. struct bgscan_learn_bss *bss;
  74. bss = os_zalloc(sizeof(*bss));
  75. if (!bss)
  76. continue;
  77. if (hwaddr_aton(buf + 4, bss->bssid) < 0) {
  78. os_free(bss);
  79. continue;
  80. }
  81. bss->freq = atoi(buf + 4 + 18);
  82. dl_list_add(&data->bss, &bss->list);
  83. wpa_printf(MSG_DEBUG, "bgscan learn: Loaded BSS "
  84. "entry: " MACSTR " freq=%d",
  85. MAC2STR(bss->bssid), bss->freq);
  86. }
  87. }
  88. fclose(f);
  89. return 0;
  90. }
  91. static void bgscan_learn_save(struct bgscan_learn_data *data)
  92. {
  93. FILE *f;
  94. struct bgscan_learn_bss *bss;
  95. if (data->fname == NULL)
  96. return;
  97. wpa_printf(MSG_DEBUG, "bgscan learn: Saving data to %s",
  98. data->fname);
  99. f = fopen(data->fname, "w");
  100. if (f == NULL)
  101. return;
  102. fprintf(f, "wpa_supplicant-bgscan-learn\n");
  103. dl_list_for_each(bss, &data->bss, struct bgscan_learn_bss, list) {
  104. fprintf(f, "BSS " MACSTR " %d\n",
  105. MAC2STR(bss->bssid), bss->freq);
  106. }
  107. fclose(f);
  108. }
  109. static int in_array(int *array, int val)
  110. {
  111. int i;
  112. if (array == NULL)
  113. return 0;
  114. for (i = 0; array[i]; i++) {
  115. if (array[i] == val)
  116. return 1;
  117. }
  118. return 0;
  119. }
  120. static int * bgscan_learn_get_freqs(struct bgscan_learn_data *data,
  121. size_t *count)
  122. {
  123. struct bgscan_learn_bss *bss;
  124. int *freqs = NULL, *n;
  125. *count = 0;
  126. dl_list_for_each(bss, &data->bss, struct bgscan_learn_bss, list) {
  127. if (in_array(freqs, bss->freq))
  128. continue;
  129. n = os_realloc(freqs, (*count + 2) * sizeof(int));
  130. if (n == NULL)
  131. return freqs;
  132. freqs = n;
  133. freqs[*count] = bss->freq;
  134. (*count)++;
  135. freqs[*count] = 0;
  136. }
  137. return freqs;
  138. }
  139. static int * bgscan_learn_get_probe_freq(struct bgscan_learn_data *data,
  140. int *freqs, size_t count)
  141. {
  142. int idx, *n;
  143. if (data->supp_freqs == NULL)
  144. return freqs;
  145. idx = data->probe_idx + 1;
  146. while (idx != data->probe_idx) {
  147. if (data->supp_freqs[idx] == 0)
  148. idx = 0;
  149. if (!in_array(freqs, data->supp_freqs[idx])) {
  150. wpa_printf(MSG_DEBUG, "bgscan learn: Probe new freq "
  151. "%u", data->supp_freqs[idx]);
  152. data->probe_idx = idx;
  153. n = os_realloc(freqs, (count + 2) * sizeof(int));
  154. if (n == NULL)
  155. return freqs;
  156. freqs = n;
  157. freqs[count] = data->supp_freqs[idx];
  158. count++;
  159. freqs[count] = 0;
  160. break;
  161. }
  162. idx++;
  163. }
  164. return freqs;
  165. }
  166. static void bgscan_learn_timeout(void *eloop_ctx, void *timeout_ctx)
  167. {
  168. struct bgscan_learn_data *data = eloop_ctx;
  169. struct wpa_supplicant *wpa_s = data->wpa_s;
  170. struct wpa_driver_scan_params params;
  171. int *freqs = NULL;
  172. size_t count, i;
  173. char msg[100], *pos;
  174. os_memset(&params, 0, sizeof(params));
  175. params.num_ssids = 1;
  176. params.ssids[0].ssid = data->ssid->ssid;
  177. params.ssids[0].ssid_len = data->ssid->ssid_len;
  178. if (data->ssid->scan_freq)
  179. params.freqs = data->ssid->scan_freq;
  180. else {
  181. freqs = bgscan_learn_get_freqs(data, &count);
  182. wpa_printf(MSG_DEBUG, "bgscan learn: BSSes in this ESS have "
  183. "been seen on %u channels", (unsigned int) count);
  184. freqs = bgscan_learn_get_probe_freq(data, freqs, count);
  185. msg[0] = '\0';
  186. pos = msg;
  187. for (i = 0; freqs && freqs[i]; i++) {
  188. int ret;
  189. ret = os_snprintf(pos, msg + sizeof(msg) - pos, " %d",
  190. freqs[i]);
  191. if (ret < 0 || ret >= msg + sizeof(msg) - pos)
  192. break;
  193. pos += ret;
  194. }
  195. pos[0] = '\0';
  196. wpa_printf(MSG_DEBUG, "bgscan learn: Scanning frequencies:%s",
  197. msg);
  198. params.freqs = freqs;
  199. }
  200. wpa_printf(MSG_DEBUG, "bgscan learn: Request a background scan");
  201. if (wpa_supplicant_trigger_scan(wpa_s, &params)) {
  202. wpa_printf(MSG_DEBUG, "bgscan learn: Failed to trigger scan");
  203. eloop_register_timeout(data->scan_interval, 0,
  204. bgscan_learn_timeout, data, NULL);
  205. } else
  206. os_get_time(&data->last_bgscan);
  207. os_free(freqs);
  208. }
  209. static int bgscan_learn_get_params(struct bgscan_learn_data *data,
  210. const char *params)
  211. {
  212. const char *pos;
  213. if (params == NULL)
  214. return 0;
  215. data->short_interval = atoi(params);
  216. pos = os_strchr(params, ':');
  217. if (pos == NULL)
  218. return 0;
  219. pos++;
  220. data->signal_threshold = atoi(pos);
  221. pos = os_strchr(pos, ':');
  222. if (pos == NULL) {
  223. wpa_printf(MSG_ERROR, "bgscan learn: Missing scan interval "
  224. "for high signal");
  225. return -1;
  226. }
  227. pos++;
  228. data->long_interval = atoi(pos);
  229. pos = os_strchr(pos, ':');
  230. if (pos) {
  231. pos++;
  232. data->fname = os_strdup(pos);
  233. }
  234. return 0;
  235. }
  236. static int * bgscan_learn_get_supp_freqs(struct wpa_supplicant *wpa_s)
  237. {
  238. struct hostapd_hw_modes *modes;
  239. u16 num_modes, flags;
  240. int i, j, *freqs = NULL, *n;
  241. size_t count = 0;
  242. modes = wpa_drv_get_hw_feature_data(wpa_s, &num_modes, &flags);
  243. if (!modes)
  244. return NULL;
  245. for (i = 0; i < num_modes; i++) {
  246. for (j = 0; j < modes[i].num_channels; j++) {
  247. if (modes[i].channels[j].flag & HOSTAPD_CHAN_DISABLED)
  248. continue;
  249. n = os_realloc(freqs, (count + 2) * sizeof(int));
  250. if (!n)
  251. continue;
  252. freqs = n;
  253. freqs[count] = modes[i].channels[j].freq;
  254. count++;
  255. freqs[count] = 0;
  256. }
  257. os_free(modes[i].channels);
  258. os_free(modes[i].rates);
  259. }
  260. os_free(modes);
  261. return freqs;
  262. }
  263. static void * bgscan_learn_init(struct wpa_supplicant *wpa_s,
  264. const char *params,
  265. const struct wpa_ssid *ssid)
  266. {
  267. struct bgscan_learn_data *data;
  268. data = os_zalloc(sizeof(*data));
  269. if (data == NULL)
  270. return NULL;
  271. dl_list_init(&data->bss);
  272. data->wpa_s = wpa_s;
  273. data->ssid = ssid;
  274. if (bgscan_learn_get_params(data, params) < 0) {
  275. os_free(data->fname);
  276. os_free(data);
  277. return NULL;
  278. }
  279. if (data->short_interval <= 0)
  280. data->short_interval = 30;
  281. if (data->long_interval <= 0)
  282. data->long_interval = 30;
  283. if (bgscan_learn_load(data) < 0) {
  284. os_free(data->fname);
  285. os_free(data);
  286. return NULL;
  287. }
  288. wpa_printf(MSG_DEBUG, "bgscan learn: Signal strength threshold %d "
  289. "Short bgscan interval %d Long bgscan interval %d",
  290. data->signal_threshold, data->short_interval,
  291. data->long_interval);
  292. if (data->signal_threshold &&
  293. wpa_drv_signal_monitor(wpa_s, data->signal_threshold, 4) < 0) {
  294. wpa_printf(MSG_ERROR, "bgscan learn: Failed to enable "
  295. "signal strength monitoring");
  296. }
  297. data->supp_freqs = bgscan_learn_get_supp_freqs(wpa_s);
  298. data->scan_interval = data->short_interval;
  299. eloop_register_timeout(data->scan_interval, 0, bgscan_learn_timeout,
  300. data, NULL);
  301. return data;
  302. }
  303. static void bgscan_learn_deinit(void *priv)
  304. {
  305. struct bgscan_learn_data *data = priv;
  306. struct bgscan_learn_bss *bss, *n;
  307. bgscan_learn_save(data);
  308. eloop_cancel_timeout(bgscan_learn_timeout, data, NULL);
  309. if (data->signal_threshold)
  310. wpa_drv_signal_monitor(data->wpa_s, 0, 0);
  311. os_free(data->fname);
  312. dl_list_for_each_safe(bss, n, &data->bss, struct bgscan_learn_bss,
  313. list) {
  314. dl_list_del(&bss->list);
  315. os_free(bss);
  316. }
  317. os_free(data->supp_freqs);
  318. os_free(data);
  319. }
  320. static int bgscan_learn_bss_match(struct bgscan_learn_data *data,
  321. struct wpa_scan_res *bss)
  322. {
  323. const u8 *ie;
  324. ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
  325. if (ie == NULL)
  326. return 0;
  327. if (data->ssid->ssid_len != ie[1] ||
  328. os_memcmp(data->ssid->ssid, ie + 2, ie[1]) != 0)
  329. return 0; /* SSID mismatch */
  330. return 1;
  331. }
  332. static int bgscan_learn_notify_scan(void *priv,
  333. struct wpa_scan_results *scan_res)
  334. {
  335. struct bgscan_learn_data *data = priv;
  336. size_t i;
  337. wpa_printf(MSG_DEBUG, "bgscan learn: scan result notification");
  338. eloop_cancel_timeout(bgscan_learn_timeout, data, NULL);
  339. eloop_register_timeout(data->scan_interval, 0, bgscan_learn_timeout,
  340. data, NULL);
  341. for (i = 0; i < scan_res->num; i++) {
  342. struct wpa_scan_res *res = scan_res->res[i];
  343. struct bgscan_learn_bss *bss;
  344. if (!bgscan_learn_bss_match(data, res))
  345. continue;
  346. bss = bgscan_learn_get_bss(data, res->bssid);
  347. if (bss && bss->freq != res->freq) {
  348. wpa_printf(MSG_DEBUG, "bgscan learn: Update BSS "
  349. MACSTR " freq %d -> %d",
  350. MAC2STR(res->bssid), bss->freq, res->freq);
  351. bss->freq = res->freq;
  352. } else if (!bss) {
  353. wpa_printf(MSG_DEBUG, "bgscan learn: Add BSS " MACSTR
  354. " freq=%d", MAC2STR(res->bssid), res->freq);
  355. bss = os_zalloc(sizeof(*bss));
  356. if (!bss)
  357. continue;
  358. os_memcpy(bss->bssid, res->bssid, ETH_ALEN);
  359. bss->freq = res->freq;
  360. dl_list_add(&data->bss, &bss->list);
  361. }
  362. }
  363. /*
  364. * A more advanced bgscan could process scan results internally, select
  365. * the BSS and request roam if needed. This sample uses the existing
  366. * BSS/ESS selection routine. Change this to return 1 if selection is
  367. * done inside the bgscan module.
  368. */
  369. return 0;
  370. }
  371. static void bgscan_learn_notify_beacon_loss(void *priv)
  372. {
  373. wpa_printf(MSG_DEBUG, "bgscan learn: beacon loss");
  374. /* TODO: speed up background scanning */
  375. }
  376. static void bgscan_learn_notify_signal_change(void *priv, int above)
  377. {
  378. struct bgscan_learn_data *data = priv;
  379. if (data->short_interval == data->long_interval ||
  380. data->signal_threshold == 0)
  381. return;
  382. wpa_printf(MSG_DEBUG, "bgscan learn: signal level changed "
  383. "(above=%d)", above);
  384. if (data->scan_interval == data->long_interval && !above) {
  385. wpa_printf(MSG_DEBUG, "bgscan learn: Trigger immediate scan "
  386. "and start using short bgscan interval");
  387. data->scan_interval = data->short_interval;
  388. eloop_cancel_timeout(bgscan_learn_timeout, data, NULL);
  389. eloop_register_timeout(0, 0, bgscan_learn_timeout, data,
  390. NULL);
  391. } else if (data->scan_interval == data->short_interval && above) {
  392. wpa_printf(MSG_DEBUG, "bgscan learn: Start using long bgscan "
  393. "interval");
  394. data->scan_interval = data->long_interval;
  395. eloop_cancel_timeout(bgscan_learn_timeout, data, NULL);
  396. eloop_register_timeout(data->scan_interval, 0,
  397. bgscan_learn_timeout, data, NULL);
  398. } else if (!above) {
  399. struct os_time now;
  400. /*
  401. * Signal dropped further 4 dB. Request a new scan if we have
  402. * not yet scanned in a while.
  403. */
  404. os_get_time(&now);
  405. if (now.sec > data->last_bgscan.sec + 10) {
  406. wpa_printf(MSG_DEBUG, "bgscan learn: Trigger "
  407. "immediate scan");
  408. eloop_cancel_timeout(bgscan_learn_timeout, data,
  409. NULL);
  410. eloop_register_timeout(0, 0, bgscan_learn_timeout,
  411. data, NULL);
  412. }
  413. }
  414. }
  415. const struct bgscan_ops bgscan_learn_ops = {
  416. .name = "learn",
  417. .init = bgscan_learn_init,
  418. .deinit = bgscan_learn_deinit,
  419. .notify_scan = bgscan_learn_notify_scan,
  420. .notify_beacon_loss = bgscan_learn_notify_beacon_loss,
  421. .notify_signal_change = bgscan_learn_notify_signal_change,
  422. };