Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

bgscan_learn.c 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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. u8 *neigh; /* num_neigh * ETH_ALEN buffer */
  30. size_t num_neigh;
  31. };
  32. struct bgscan_learn_data {
  33. struct wpa_supplicant *wpa_s;
  34. const struct wpa_ssid *ssid;
  35. int scan_interval;
  36. int signal_threshold;
  37. int short_interval; /* use if signal < threshold */
  38. int long_interval; /* use if signal > threshold */
  39. struct os_time last_bgscan;
  40. char *fname;
  41. struct dl_list bss;
  42. int *supp_freqs;
  43. int probe_idx;
  44. };
  45. static void bss_free(struct bgscan_learn_bss *bss)
  46. {
  47. os_free(bss->neigh);
  48. os_free(bss);
  49. }
  50. static int bssid_in_array(u8 *array, size_t array_len, const u8 *bssid)
  51. {
  52. size_t i;
  53. if (array == NULL || array_len == 0)
  54. return 0;
  55. for (i = 0; i < array_len; i++) {
  56. if (os_memcmp(array + i * ETH_ALEN, bssid, ETH_ALEN) == 0)
  57. return 1;
  58. }
  59. return 0;
  60. }
  61. static void bgscan_learn_add_neighbor(struct bgscan_learn_bss *bss,
  62. const u8 *bssid)
  63. {
  64. u8 *n;
  65. if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
  66. return;
  67. if (bssid_in_array(bss->neigh, bss->num_neigh, bssid))
  68. return;
  69. n = os_realloc(bss->neigh, (bss->num_neigh + 1) * ETH_ALEN);
  70. if (n == NULL)
  71. return;
  72. os_memcpy(n + bss->num_neigh * ETH_ALEN, bssid, ETH_ALEN);
  73. bss->neigh = n;
  74. bss->num_neigh++;
  75. }
  76. static struct bgscan_learn_bss * bgscan_learn_get_bss(
  77. struct bgscan_learn_data *data, const u8 *bssid)
  78. {
  79. struct bgscan_learn_bss *bss;
  80. dl_list_for_each(bss, &data->bss, struct bgscan_learn_bss, list) {
  81. if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
  82. return bss;
  83. }
  84. return NULL;
  85. }
  86. static int bgscan_learn_load(struct bgscan_learn_data *data)
  87. {
  88. FILE *f;
  89. char buf[128];
  90. struct bgscan_learn_bss *bss;
  91. if (data->fname == NULL)
  92. return 0;
  93. f = fopen(data->fname, "r");
  94. if (f == NULL)
  95. return 0;
  96. wpa_printf(MSG_DEBUG, "bgscan learn: Loading data from %s",
  97. data->fname);
  98. if (fgets(buf, sizeof(buf), f) == NULL ||
  99. os_strncmp(buf, "wpa_supplicant-bgscan-learn\n", 28) != 0) {
  100. wpa_printf(MSG_INFO, "bgscan learn: Invalid data file %s",
  101. data->fname);
  102. fclose(f);
  103. return -1;
  104. }
  105. while (fgets(buf, sizeof(buf), f)) {
  106. if (os_strncmp(buf, "BSS ", 4) == 0) {
  107. bss = os_zalloc(sizeof(*bss));
  108. if (!bss)
  109. continue;
  110. if (hwaddr_aton(buf + 4, bss->bssid) < 0) {
  111. bss_free(bss);
  112. continue;
  113. }
  114. bss->freq = atoi(buf + 4 + 18);
  115. dl_list_add(&data->bss, &bss->list);
  116. wpa_printf(MSG_DEBUG, "bgscan learn: Loaded BSS "
  117. "entry: " MACSTR " freq=%d",
  118. MAC2STR(bss->bssid), bss->freq);
  119. }
  120. if (os_strncmp(buf, "NEIGHBOR ", 9) == 0) {
  121. u8 addr[ETH_ALEN];
  122. if (hwaddr_aton(buf + 9, addr) < 0)
  123. continue;
  124. bss = bgscan_learn_get_bss(data, addr);
  125. if (bss == NULL)
  126. continue;
  127. if (hwaddr_aton(buf + 9 + 18, addr) < 0)
  128. continue;
  129. bgscan_learn_add_neighbor(bss, addr);
  130. }
  131. }
  132. fclose(f);
  133. return 0;
  134. }
  135. static void bgscan_learn_save(struct bgscan_learn_data *data)
  136. {
  137. FILE *f;
  138. struct bgscan_learn_bss *bss;
  139. if (data->fname == NULL)
  140. return;
  141. wpa_printf(MSG_DEBUG, "bgscan learn: Saving data to %s",
  142. data->fname);
  143. f = fopen(data->fname, "w");
  144. if (f == NULL)
  145. return;
  146. fprintf(f, "wpa_supplicant-bgscan-learn\n");
  147. dl_list_for_each(bss, &data->bss, struct bgscan_learn_bss, list) {
  148. fprintf(f, "BSS " MACSTR " %d\n",
  149. MAC2STR(bss->bssid), bss->freq);
  150. }
  151. dl_list_for_each(bss, &data->bss, struct bgscan_learn_bss, list) {
  152. size_t i;
  153. for (i = 0; i < bss->num_neigh; i++) {
  154. fprintf(f, "NEIGHBOR " MACSTR " " MACSTR "\n",
  155. MAC2STR(bss->bssid),
  156. MAC2STR(bss->neigh + i * ETH_ALEN));
  157. }
  158. }
  159. fclose(f);
  160. }
  161. static int in_array(int *array, int val)
  162. {
  163. int i;
  164. if (array == NULL)
  165. return 0;
  166. for (i = 0; array[i]; i++) {
  167. if (array[i] == val)
  168. return 1;
  169. }
  170. return 0;
  171. }
  172. static int * bgscan_learn_get_freqs(struct bgscan_learn_data *data,
  173. size_t *count)
  174. {
  175. struct bgscan_learn_bss *bss;
  176. int *freqs = NULL, *n;
  177. *count = 0;
  178. dl_list_for_each(bss, &data->bss, struct bgscan_learn_bss, list) {
  179. if (in_array(freqs, bss->freq))
  180. continue;
  181. n = os_realloc(freqs, (*count + 2) * sizeof(int));
  182. if (n == NULL)
  183. return freqs;
  184. freqs = n;
  185. freqs[*count] = bss->freq;
  186. (*count)++;
  187. freqs[*count] = 0;
  188. }
  189. return freqs;
  190. }
  191. static int * bgscan_learn_get_probe_freq(struct bgscan_learn_data *data,
  192. int *freqs, size_t count)
  193. {
  194. int idx, *n;
  195. if (data->supp_freqs == NULL)
  196. return freqs;
  197. idx = data->probe_idx + 1;
  198. while (idx != data->probe_idx) {
  199. if (data->supp_freqs[idx] == 0)
  200. idx = 0;
  201. if (!in_array(freqs, data->supp_freqs[idx])) {
  202. wpa_printf(MSG_DEBUG, "bgscan learn: Probe new freq "
  203. "%u", data->supp_freqs[idx]);
  204. data->probe_idx = idx;
  205. n = os_realloc(freqs, (count + 2) * sizeof(int));
  206. if (n == NULL)
  207. return freqs;
  208. freqs = n;
  209. freqs[count] = data->supp_freqs[idx];
  210. count++;
  211. freqs[count] = 0;
  212. break;
  213. }
  214. idx++;
  215. }
  216. return freqs;
  217. }
  218. static void bgscan_learn_timeout(void *eloop_ctx, void *timeout_ctx)
  219. {
  220. struct bgscan_learn_data *data = eloop_ctx;
  221. struct wpa_supplicant *wpa_s = data->wpa_s;
  222. struct wpa_driver_scan_params params;
  223. int *freqs = NULL;
  224. size_t count, i;
  225. char msg[100], *pos;
  226. os_memset(&params, 0, sizeof(params));
  227. params.num_ssids = 1;
  228. params.ssids[0].ssid = data->ssid->ssid;
  229. params.ssids[0].ssid_len = data->ssid->ssid_len;
  230. if (data->ssid->scan_freq)
  231. params.freqs = data->ssid->scan_freq;
  232. else {
  233. freqs = bgscan_learn_get_freqs(data, &count);
  234. wpa_printf(MSG_DEBUG, "bgscan learn: BSSes in this ESS have "
  235. "been seen on %u channels", (unsigned int) count);
  236. freqs = bgscan_learn_get_probe_freq(data, freqs, count);
  237. msg[0] = '\0';
  238. pos = msg;
  239. for (i = 0; freqs && freqs[i]; i++) {
  240. int ret;
  241. ret = os_snprintf(pos, msg + sizeof(msg) - pos, " %d",
  242. freqs[i]);
  243. if (ret < 0 || ret >= msg + sizeof(msg) - pos)
  244. break;
  245. pos += ret;
  246. }
  247. pos[0] = '\0';
  248. wpa_printf(MSG_DEBUG, "bgscan learn: Scanning frequencies:%s",
  249. msg);
  250. params.freqs = freqs;
  251. }
  252. wpa_printf(MSG_DEBUG, "bgscan learn: Request a background scan");
  253. if (wpa_supplicant_trigger_scan(wpa_s, &params)) {
  254. wpa_printf(MSG_DEBUG, "bgscan learn: Failed to trigger scan");
  255. eloop_register_timeout(data->scan_interval, 0,
  256. bgscan_learn_timeout, data, NULL);
  257. } else
  258. os_get_time(&data->last_bgscan);
  259. os_free(freqs);
  260. }
  261. static int bgscan_learn_get_params(struct bgscan_learn_data *data,
  262. const char *params)
  263. {
  264. const char *pos;
  265. if (params == NULL)
  266. return 0;
  267. data->short_interval = atoi(params);
  268. pos = os_strchr(params, ':');
  269. if (pos == NULL)
  270. return 0;
  271. pos++;
  272. data->signal_threshold = atoi(pos);
  273. pos = os_strchr(pos, ':');
  274. if (pos == NULL) {
  275. wpa_printf(MSG_ERROR, "bgscan learn: Missing scan interval "
  276. "for high signal");
  277. return -1;
  278. }
  279. pos++;
  280. data->long_interval = atoi(pos);
  281. pos = os_strchr(pos, ':');
  282. if (pos) {
  283. pos++;
  284. data->fname = os_strdup(pos);
  285. }
  286. return 0;
  287. }
  288. static int * bgscan_learn_get_supp_freqs(struct wpa_supplicant *wpa_s)
  289. {
  290. struct hostapd_hw_modes *modes;
  291. u16 num_modes, flags;
  292. int i, j, *freqs = NULL, *n;
  293. size_t count = 0;
  294. modes = wpa_drv_get_hw_feature_data(wpa_s, &num_modes, &flags);
  295. if (!modes)
  296. return NULL;
  297. for (i = 0; i < num_modes; i++) {
  298. for (j = 0; j < modes[i].num_channels; j++) {
  299. if (modes[i].channels[j].flag & HOSTAPD_CHAN_DISABLED)
  300. continue;
  301. n = os_realloc(freqs, (count + 2) * sizeof(int));
  302. if (!n)
  303. continue;
  304. freqs = n;
  305. freqs[count] = modes[i].channels[j].freq;
  306. count++;
  307. freqs[count] = 0;
  308. }
  309. os_free(modes[i].channels);
  310. os_free(modes[i].rates);
  311. }
  312. os_free(modes);
  313. return freqs;
  314. }
  315. static void * bgscan_learn_init(struct wpa_supplicant *wpa_s,
  316. const char *params,
  317. const struct wpa_ssid *ssid)
  318. {
  319. struct bgscan_learn_data *data;
  320. data = os_zalloc(sizeof(*data));
  321. if (data == NULL)
  322. return NULL;
  323. dl_list_init(&data->bss);
  324. data->wpa_s = wpa_s;
  325. data->ssid = ssid;
  326. if (bgscan_learn_get_params(data, params) < 0) {
  327. os_free(data->fname);
  328. os_free(data);
  329. return NULL;
  330. }
  331. if (data->short_interval <= 0)
  332. data->short_interval = 30;
  333. if (data->long_interval <= 0)
  334. data->long_interval = 30;
  335. if (bgscan_learn_load(data) < 0) {
  336. os_free(data->fname);
  337. os_free(data);
  338. return NULL;
  339. }
  340. wpa_printf(MSG_DEBUG, "bgscan learn: Signal strength threshold %d "
  341. "Short bgscan interval %d Long bgscan interval %d",
  342. data->signal_threshold, data->short_interval,
  343. data->long_interval);
  344. if (data->signal_threshold &&
  345. wpa_drv_signal_monitor(wpa_s, data->signal_threshold, 4) < 0) {
  346. wpa_printf(MSG_ERROR, "bgscan learn: Failed to enable "
  347. "signal strength monitoring");
  348. }
  349. data->supp_freqs = bgscan_learn_get_supp_freqs(wpa_s);
  350. data->scan_interval = data->short_interval;
  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_time(&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_time 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_time(&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_time(&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. };