bgscan_learn.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. };
  41. static struct bgscan_learn_bss * bgscan_learn_get_bss(
  42. struct bgscan_learn_data *data, const u8 *bssid)
  43. {
  44. struct bgscan_learn_bss *bss;
  45. dl_list_for_each(bss, &data->bss, struct bgscan_learn_bss, list) {
  46. if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
  47. return bss;
  48. }
  49. return NULL;
  50. }
  51. static int bgscan_learn_load(struct bgscan_learn_data *data)
  52. {
  53. FILE *f;
  54. char buf[128];
  55. if (data->fname == NULL)
  56. return 0;
  57. f = fopen(data->fname, "r");
  58. if (f == NULL)
  59. return 0;
  60. wpa_printf(MSG_DEBUG, "bgscan learn: Loading data from %s",
  61. data->fname);
  62. if (fgets(buf, sizeof(buf), f) == NULL ||
  63. os_strncmp(buf, "wpa_supplicant-bgscan-learn\n", 28) != 0) {
  64. wpa_printf(MSG_INFO, "bgscan learn: Invalid data file %s",
  65. data->fname);
  66. fclose(f);
  67. return -1;
  68. }
  69. while (fgets(buf, sizeof(buf), f)) {
  70. if (os_strncmp(buf, "BSS ", 4) == 0) {
  71. struct bgscan_learn_bss *bss;
  72. bss = os_zalloc(sizeof(*bss));
  73. if (!bss)
  74. continue;
  75. if (hwaddr_aton(buf + 4, bss->bssid) < 0) {
  76. os_free(bss);
  77. continue;
  78. }
  79. bss->freq = atoi(buf + 4 + 18);
  80. dl_list_add(&data->bss, &bss->list);
  81. wpa_printf(MSG_DEBUG, "bgscan learn: Loaded BSS "
  82. "entry: " MACSTR " freq=%d",
  83. MAC2STR(bss->bssid), bss->freq);
  84. }
  85. }
  86. fclose(f);
  87. return 0;
  88. }
  89. static void bgscan_learn_save(struct bgscan_learn_data *data)
  90. {
  91. FILE *f;
  92. struct bgscan_learn_bss *bss;
  93. if (data->fname == NULL)
  94. return;
  95. wpa_printf(MSG_DEBUG, "bgscan learn: Saving data to %s",
  96. data->fname);
  97. f = fopen(data->fname, "w");
  98. if (f == NULL)
  99. return;
  100. fprintf(f, "wpa_supplicant-bgscan-learn\n");
  101. dl_list_for_each(bss, &data->bss, struct bgscan_learn_bss, list) {
  102. fprintf(f, "BSS " MACSTR " %d\n",
  103. MAC2STR(bss->bssid), bss->freq);
  104. }
  105. fclose(f);
  106. }
  107. static int in_array(int *array, int val)
  108. {
  109. int i;
  110. if (array == NULL)
  111. return 0;
  112. for (i = 0; array[i]; i++) {
  113. if (array[i] == val)
  114. return 1;
  115. }
  116. return 0;
  117. }
  118. static int * bgscan_learn_get_freqs(struct bgscan_learn_data *data,
  119. size_t *count)
  120. {
  121. struct bgscan_learn_bss *bss;
  122. int *freqs = NULL, *n;
  123. *count = 0;
  124. dl_list_for_each(bss, &data->bss, struct bgscan_learn_bss, list) {
  125. if (in_array(freqs, bss->freq))
  126. continue;
  127. n = os_realloc(freqs, (*count + 2) * sizeof(int));
  128. if (n == NULL)
  129. return freqs;
  130. freqs = n;
  131. freqs[*count] = bss->freq;
  132. (*count)++;
  133. freqs[*count] = 0;
  134. }
  135. return freqs;
  136. }
  137. static void bgscan_learn_timeout(void *eloop_ctx, void *timeout_ctx)
  138. {
  139. struct bgscan_learn_data *data = eloop_ctx;
  140. struct wpa_supplicant *wpa_s = data->wpa_s;
  141. struct wpa_driver_scan_params params;
  142. int *freqs = NULL;
  143. size_t count;
  144. os_memset(&params, 0, sizeof(params));
  145. params.num_ssids = 1;
  146. params.ssids[0].ssid = data->ssid->ssid;
  147. params.ssids[0].ssid_len = data->ssid->ssid_len;
  148. if (data->ssid->scan_freq)
  149. params.freqs = data->ssid->scan_freq;
  150. else {
  151. freqs = bgscan_learn_get_freqs(data, &count);
  152. wpa_printf(MSG_DEBUG, "bgscan learn: BSSes in this ESS have "
  153. "been seen on %u channels", (unsigned int) count);
  154. /*
  155. * TODO: add other frequencies (rotate through one or couple at
  156. * a time, etc., to find APs from new channels)
  157. */
  158. params.freqs = freqs;
  159. }
  160. wpa_printf(MSG_DEBUG, "bgscan learn: Request a background scan");
  161. if (wpa_supplicant_trigger_scan(wpa_s, &params)) {
  162. wpa_printf(MSG_DEBUG, "bgscan learn: Failed to trigger scan");
  163. eloop_register_timeout(data->scan_interval, 0,
  164. bgscan_learn_timeout, data, NULL);
  165. } else
  166. os_get_time(&data->last_bgscan);
  167. os_free(freqs);
  168. }
  169. static int bgscan_learn_get_params(struct bgscan_learn_data *data,
  170. const char *params)
  171. {
  172. const char *pos;
  173. if (params == NULL)
  174. return 0;
  175. data->short_interval = atoi(params);
  176. pos = os_strchr(params, ':');
  177. if (pos == NULL)
  178. return 0;
  179. pos++;
  180. data->signal_threshold = atoi(pos);
  181. pos = os_strchr(pos, ':');
  182. if (pos == NULL) {
  183. wpa_printf(MSG_ERROR, "bgscan learn: Missing scan interval "
  184. "for high signal");
  185. return -1;
  186. }
  187. pos++;
  188. data->long_interval = atoi(pos);
  189. pos = os_strchr(pos, ':');
  190. if (pos) {
  191. pos++;
  192. data->fname = os_strdup(pos);
  193. }
  194. return 0;
  195. }
  196. static void * bgscan_learn_init(struct wpa_supplicant *wpa_s,
  197. const char *params,
  198. const struct wpa_ssid *ssid)
  199. {
  200. struct bgscan_learn_data *data;
  201. data = os_zalloc(sizeof(*data));
  202. if (data == NULL)
  203. return NULL;
  204. dl_list_init(&data->bss);
  205. data->wpa_s = wpa_s;
  206. data->ssid = ssid;
  207. if (bgscan_learn_get_params(data, params) < 0) {
  208. os_free(data->fname);
  209. os_free(data);
  210. return NULL;
  211. }
  212. if (data->short_interval <= 0)
  213. data->short_interval = 30;
  214. if (data->long_interval <= 0)
  215. data->long_interval = 30;
  216. if (bgscan_learn_load(data) < 0) {
  217. os_free(data->fname);
  218. os_free(data);
  219. return NULL;
  220. }
  221. wpa_printf(MSG_DEBUG, "bgscan learn: Signal strength threshold %d "
  222. "Short bgscan interval %d Long bgscan interval %d",
  223. data->signal_threshold, data->short_interval,
  224. data->long_interval);
  225. if (data->signal_threshold &&
  226. wpa_drv_signal_monitor(wpa_s, data->signal_threshold, 4) < 0) {
  227. wpa_printf(MSG_ERROR, "bgscan learn: Failed to enable "
  228. "signal strength monitoring");
  229. }
  230. data->scan_interval = data->short_interval;
  231. eloop_register_timeout(data->scan_interval, 0, bgscan_learn_timeout,
  232. data, NULL);
  233. return data;
  234. }
  235. static void bgscan_learn_deinit(void *priv)
  236. {
  237. struct bgscan_learn_data *data = priv;
  238. struct bgscan_learn_bss *bss, *n;
  239. bgscan_learn_save(data);
  240. eloop_cancel_timeout(bgscan_learn_timeout, data, NULL);
  241. if (data->signal_threshold)
  242. wpa_drv_signal_monitor(data->wpa_s, 0, 0);
  243. os_free(data->fname);
  244. dl_list_for_each_safe(bss, n, &data->bss, struct bgscan_learn_bss,
  245. list) {
  246. dl_list_del(&bss->list);
  247. os_free(bss);
  248. }
  249. os_free(data);
  250. }
  251. static int bgscan_learn_bss_match(struct bgscan_learn_data *data,
  252. struct wpa_scan_res *bss)
  253. {
  254. const u8 *ie;
  255. ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
  256. if (ie == NULL)
  257. return 0;
  258. if (data->ssid->ssid_len != ie[1] ||
  259. os_memcmp(data->ssid->ssid, ie + 2, ie[1]) != 0)
  260. return 0; /* SSID mismatch */
  261. return 1;
  262. }
  263. static int bgscan_learn_notify_scan(void *priv,
  264. struct wpa_scan_results *scan_res)
  265. {
  266. struct bgscan_learn_data *data = priv;
  267. size_t i;
  268. wpa_printf(MSG_DEBUG, "bgscan learn: scan result notification");
  269. eloop_cancel_timeout(bgscan_learn_timeout, data, NULL);
  270. eloop_register_timeout(data->scan_interval, 0, bgscan_learn_timeout,
  271. data, NULL);
  272. for (i = 0; i < scan_res->num; i++) {
  273. struct wpa_scan_res *res = scan_res->res[i];
  274. struct bgscan_learn_bss *bss;
  275. if (!bgscan_learn_bss_match(data, res))
  276. continue;
  277. bss = bgscan_learn_get_bss(data, res->bssid);
  278. if (bss && bss->freq != res->freq) {
  279. wpa_printf(MSG_DEBUG, "bgscan learn: Update BSS "
  280. MACSTR " freq %d -> %d",
  281. MAC2STR(res->bssid), bss->freq, res->freq);
  282. bss->freq = res->freq;
  283. } else if (!bss) {
  284. wpa_printf(MSG_DEBUG, "bgscan learn: Add BSS " MACSTR
  285. " freq=%d", MAC2STR(res->bssid), res->freq);
  286. bss = os_zalloc(sizeof(*bss));
  287. if (!bss)
  288. continue;
  289. os_memcpy(bss->bssid, res->bssid, ETH_ALEN);
  290. bss->freq = res->freq;
  291. dl_list_add(&data->bss, &bss->list);
  292. }
  293. }
  294. /*
  295. * A more advanced bgscan could process scan results internally, select
  296. * the BSS and request roam if needed. This sample uses the existing
  297. * BSS/ESS selection routine. Change this to return 1 if selection is
  298. * done inside the bgscan module.
  299. */
  300. return 0;
  301. }
  302. static void bgscan_learn_notify_beacon_loss(void *priv)
  303. {
  304. wpa_printf(MSG_DEBUG, "bgscan learn: beacon loss");
  305. /* TODO: speed up background scanning */
  306. }
  307. static void bgscan_learn_notify_signal_change(void *priv, int above)
  308. {
  309. struct bgscan_learn_data *data = priv;
  310. if (data->short_interval == data->long_interval ||
  311. data->signal_threshold == 0)
  312. return;
  313. wpa_printf(MSG_DEBUG, "bgscan learn: signal level changed "
  314. "(above=%d)", above);
  315. if (data->scan_interval == data->long_interval && !above) {
  316. wpa_printf(MSG_DEBUG, "bgscan learn: Trigger immediate scan "
  317. "and start using short bgscan interval");
  318. data->scan_interval = data->short_interval;
  319. eloop_cancel_timeout(bgscan_learn_timeout, data, NULL);
  320. eloop_register_timeout(0, 0, bgscan_learn_timeout, data,
  321. NULL);
  322. } else if (data->scan_interval == data->short_interval && above) {
  323. wpa_printf(MSG_DEBUG, "bgscan learn: Start using long bgscan "
  324. "interval");
  325. data->scan_interval = data->long_interval;
  326. eloop_cancel_timeout(bgscan_learn_timeout, data, NULL);
  327. eloop_register_timeout(data->scan_interval, 0,
  328. bgscan_learn_timeout, data, NULL);
  329. } else if (!above) {
  330. struct os_time now;
  331. /*
  332. * Signal dropped further 4 dB. Request a new scan if we have
  333. * not yet scanned in a while.
  334. */
  335. os_get_time(&now);
  336. if (now.sec > data->last_bgscan.sec + 10) {
  337. wpa_printf(MSG_DEBUG, "bgscan learn: Trigger "
  338. "immediate scan");
  339. eloop_cancel_timeout(bgscan_learn_timeout, data,
  340. NULL);
  341. eloop_register_timeout(0, 0, bgscan_learn_timeout,
  342. data, NULL);
  343. }
  344. }
  345. }
  346. const struct bgscan_ops bgscan_learn_ops = {
  347. .name = "learn",
  348. .init = bgscan_learn_init,
  349. .deinit = bgscan_learn_deinit,
  350. .notify_scan = bgscan_learn_notify_scan,
  351. .notify_beacon_loss = bgscan_learn_notify_beacon_loss,
  352. .notify_signal_change = bgscan_learn_notify_signal_change,
  353. };