bgscan_learn.c 14 KB

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