scan.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. /*
  2. * WPA Supplicant - Scanning
  3. * Copyright (c) 2003-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 "utils/includes.h"
  15. #include "utils/common.h"
  16. #include "utils/eloop.h"
  17. #include "common/ieee802_11_defs.h"
  18. #include "config.h"
  19. #include "wpa_supplicant_i.h"
  20. #include "driver_i.h"
  21. #include "mlme.h"
  22. #include "wps_supplicant.h"
  23. #include "notify.h"
  24. #include "bss.h"
  25. #include "scan.h"
  26. static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
  27. {
  28. struct wpa_ssid *ssid;
  29. union wpa_event_data data;
  30. ssid = wpa_supplicant_get_ssid(wpa_s);
  31. if (ssid == NULL)
  32. return;
  33. if (wpa_s->current_ssid == NULL) {
  34. wpa_s->current_ssid = ssid;
  35. if (wpa_s->current_ssid != NULL)
  36. wpas_notify_network_changed(wpa_s);
  37. }
  38. wpa_supplicant_initiate_eapol(wpa_s);
  39. wpa_printf(MSG_DEBUG, "Already associated with a configured network - "
  40. "generating associated event");
  41. os_memset(&data, 0, sizeof(data));
  42. wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
  43. }
  44. #ifdef CONFIG_WPS
  45. static int wpas_wps_in_use(struct wpa_config *conf,
  46. enum wps_request_type *req_type)
  47. {
  48. struct wpa_ssid *ssid;
  49. int wps = 0;
  50. for (ssid = conf->ssid; ssid; ssid = ssid->next) {
  51. if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
  52. continue;
  53. wps = 1;
  54. *req_type = wpas_wps_get_req_type(ssid);
  55. if (!ssid->eap.phase1)
  56. continue;
  57. if (os_strstr(ssid->eap.phase1, "pbc=1"))
  58. return 2;
  59. }
  60. return wps;
  61. }
  62. #endif /* CONFIG_WPS */
  63. int wpa_supplicant_enabled_networks(struct wpa_config *conf)
  64. {
  65. struct wpa_ssid *ssid = conf->ssid;
  66. while (ssid) {
  67. if (!ssid->disabled)
  68. return 1;
  69. ssid = ssid->next;
  70. }
  71. return 0;
  72. }
  73. static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
  74. struct wpa_ssid *ssid)
  75. {
  76. while (ssid) {
  77. if (!ssid->disabled)
  78. break;
  79. ssid = ssid->next;
  80. }
  81. /* ap_scan=2 mode - try to associate with each SSID. */
  82. if (ssid == NULL) {
  83. wpa_printf(MSG_DEBUG, "wpa_supplicant_scan: Reached "
  84. "end of scan list - go back to beginning");
  85. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  86. wpa_supplicant_req_scan(wpa_s, 0, 0);
  87. return;
  88. }
  89. if (ssid->next) {
  90. /* Continue from the next SSID on the next attempt. */
  91. wpa_s->prev_scan_ssid = ssid;
  92. } else {
  93. /* Start from the beginning of the SSID list. */
  94. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  95. }
  96. wpa_supplicant_associate(wpa_s, NULL, ssid);
  97. }
  98. static int int_array_len(const int *a)
  99. {
  100. int i;
  101. for (i = 0; a && a[i]; i++)
  102. ;
  103. return i;
  104. }
  105. static void int_array_concat(int **res, const int *a)
  106. {
  107. int reslen, alen, i;
  108. int *n;
  109. reslen = int_array_len(*res);
  110. alen = int_array_len(a);
  111. n = os_realloc(*res, (reslen + alen + 1) * sizeof(int));
  112. if (n == NULL) {
  113. os_free(*res);
  114. *res = NULL;
  115. return;
  116. }
  117. for (i = 0; i <= alen; i++)
  118. n[reslen + i] = a[i];
  119. *res = n;
  120. }
  121. static int freq_cmp(const void *a, const void *b)
  122. {
  123. int _a = *(int *) a;
  124. int _b = *(int *) b;
  125. if (_a == 0)
  126. return 1;
  127. if (_b == 0)
  128. return -1;
  129. return _a - _b;
  130. }
  131. static void int_array_sort_unique(int *a)
  132. {
  133. int alen;
  134. int i, j;
  135. if (a == NULL)
  136. return;
  137. alen = int_array_len(a);
  138. qsort(a, alen, sizeof(int), freq_cmp);
  139. i = 0;
  140. j = 1;
  141. while (a[i] && a[j]) {
  142. if (a[i] == a[j]) {
  143. j++;
  144. continue;
  145. }
  146. a[++i] = a[j++];
  147. }
  148. if (a[i])
  149. i++;
  150. a[i] = 0;
  151. }
  152. int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
  153. struct wpa_driver_scan_params *params)
  154. {
  155. int ret;
  156. wpa_supplicant_notify_scanning(wpa_s, 1);
  157. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  158. ret = ieee80211_sta_req_scan(wpa_s, params);
  159. else
  160. ret = wpa_drv_scan(wpa_s, params);
  161. if (ret) {
  162. wpa_supplicant_notify_scanning(wpa_s, 0);
  163. wpas_notify_scan_done(wpa_s, 0);
  164. } else
  165. wpa_s->scan_runs++;
  166. return ret;
  167. }
  168. static struct wpa_driver_scan_filter *
  169. wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
  170. {
  171. struct wpa_driver_scan_filter *ssids;
  172. struct wpa_ssid *ssid;
  173. size_t count;
  174. *num_ssids = 0;
  175. if (!conf->filter_ssids)
  176. return NULL;
  177. for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
  178. if (ssid->ssid && ssid->ssid_len)
  179. count++;
  180. }
  181. if (count == 0)
  182. return NULL;
  183. ssids = os_zalloc(count * sizeof(struct wpa_driver_scan_filter));
  184. if (ssids == NULL)
  185. return NULL;
  186. for (ssid = conf->ssid; ssid; ssid = ssid->next) {
  187. if (!ssid->ssid || !ssid->ssid_len)
  188. continue;
  189. os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
  190. ssids[*num_ssids].ssid_len = ssid->ssid_len;
  191. (*num_ssids)++;
  192. }
  193. return ssids;
  194. }
  195. static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
  196. {
  197. struct wpa_supplicant *wpa_s = eloop_ctx;
  198. struct wpa_ssid *ssid;
  199. int scan_req = 0, ret;
  200. struct wpabuf *wps_ie = NULL;
  201. int wps = 0;
  202. #ifdef CONFIG_WPS
  203. enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
  204. #endif /* CONFIG_WPS */
  205. struct wpa_driver_scan_params params;
  206. size_t max_ssids;
  207. enum wpa_states prev_state;
  208. if (wpa_s->disconnected && !wpa_s->scan_req) {
  209. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  210. return;
  211. }
  212. if (!wpa_supplicant_enabled_networks(wpa_s->conf) &&
  213. !wpa_s->scan_req) {
  214. wpa_printf(MSG_DEBUG, "No enabled networks - do not scan");
  215. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  216. return;
  217. }
  218. if (wpa_s->conf->ap_scan != 0 &&
  219. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
  220. wpa_printf(MSG_DEBUG, "Using wired authentication - "
  221. "overriding ap_scan configuration");
  222. wpa_s->conf->ap_scan = 0;
  223. wpas_notify_ap_scan_changed(wpa_s);
  224. }
  225. if (wpa_s->conf->ap_scan == 0) {
  226. wpa_supplicant_gen_assoc_event(wpa_s);
  227. return;
  228. }
  229. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
  230. wpa_s->conf->ap_scan == 2)
  231. max_ssids = 1;
  232. else {
  233. max_ssids = wpa_s->max_scan_ssids;
  234. if (max_ssids > WPAS_MAX_SCAN_SSIDS)
  235. max_ssids = WPAS_MAX_SCAN_SSIDS;
  236. }
  237. #ifdef CONFIG_WPS
  238. wps = wpas_wps_in_use(wpa_s->conf, &req_type);
  239. #endif /* CONFIG_WPS */
  240. if (wpa_s->scan_res_tried == 0 && wpa_s->conf->ap_scan == 1 &&
  241. !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) &&
  242. wps != 2 && !wpa_s->conf->filter_ssids &&
  243. !wpa_s->connect_without_scan) {
  244. wpa_s->scan_res_tried++;
  245. wpa_printf(MSG_DEBUG, "Trying to get current scan results "
  246. "first without requesting a new scan to speed up "
  247. "initial association");
  248. wpa_supplicant_event(wpa_s, EVENT_SCAN_RESULTS, NULL);
  249. return;
  250. }
  251. scan_req = wpa_s->scan_req;
  252. wpa_s->scan_req = 0;
  253. os_memset(&params, 0, sizeof(params));
  254. prev_state = wpa_s->wpa_state;
  255. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  256. wpa_s->wpa_state == WPA_INACTIVE)
  257. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  258. /* Find the starting point from which to continue scanning */
  259. ssid = wpa_s->conf->ssid;
  260. if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
  261. while (ssid) {
  262. if (ssid == wpa_s->prev_scan_ssid) {
  263. ssid = ssid->next;
  264. break;
  265. }
  266. ssid = ssid->next;
  267. }
  268. }
  269. if (scan_req != 2 && (wpa_s->conf->ap_scan == 2 ||
  270. wpa_s->connect_without_scan)) {
  271. wpa_s->connect_without_scan = 0;
  272. wpa_supplicant_assoc_try(wpa_s, ssid);
  273. return;
  274. } else if (wpa_s->conf->ap_scan == 2) {
  275. /*
  276. * User-initiated scan request in ap_scan == 2; scan with
  277. * wildcard SSID.
  278. */
  279. ssid = NULL;
  280. } else {
  281. struct wpa_ssid *start = ssid, *tssid;
  282. int freqs_set = 0;
  283. if (ssid == NULL && max_ssids > 1)
  284. ssid = wpa_s->conf->ssid;
  285. while (ssid) {
  286. if (!ssid->disabled && ssid->scan_ssid) {
  287. wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
  288. ssid->ssid, ssid->ssid_len);
  289. params.ssids[params.num_ssids].ssid =
  290. ssid->ssid;
  291. params.ssids[params.num_ssids].ssid_len =
  292. ssid->ssid_len;
  293. params.num_ssids++;
  294. if (params.num_ssids + 1 >= max_ssids)
  295. break;
  296. }
  297. ssid = ssid->next;
  298. if (ssid == start)
  299. break;
  300. if (ssid == NULL && max_ssids > 1 &&
  301. start != wpa_s->conf->ssid)
  302. ssid = wpa_s->conf->ssid;
  303. }
  304. for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
  305. if (tssid->disabled)
  306. continue;
  307. if ((params.freqs || !freqs_set) && tssid->scan_freq) {
  308. int_array_concat(&params.freqs,
  309. tssid->scan_freq);
  310. } else {
  311. os_free(params.freqs);
  312. params.freqs = NULL;
  313. }
  314. freqs_set = 1;
  315. }
  316. int_array_sort_unique(params.freqs);
  317. }
  318. if (ssid) {
  319. wpa_s->prev_scan_ssid = ssid;
  320. if (max_ssids > 1) {
  321. wpa_printf(MSG_DEBUG, "Include wildcard SSID in the "
  322. "scan request");
  323. params.num_ssids++;
  324. }
  325. wpa_printf(MSG_DEBUG, "Starting AP scan for specific SSID(s)");
  326. } else {
  327. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  328. params.num_ssids++;
  329. wpa_printf(MSG_DEBUG, "Starting AP scan for wildcard SSID");
  330. }
  331. #ifdef CONFIG_WPS
  332. if (params.freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
  333. /*
  334. * Optimize post-provisioning scan based on channel used
  335. * during provisioning.
  336. */
  337. wpa_printf(MSG_DEBUG, "WPS: Scan only frequency %u MHz that "
  338. "was used during provisioning", wpa_s->wps_freq);
  339. params.freqs = os_zalloc(2 * sizeof(int));
  340. if (params.freqs)
  341. params.freqs[0] = wpa_s->wps_freq;
  342. wpa_s->after_wps--;
  343. }
  344. if (wps) {
  345. wps_ie = wps_build_probe_req_ie(wps == 2, &wpa_s->wps->dev,
  346. wpa_s->wps->uuid, req_type);
  347. if (wps_ie) {
  348. params.extra_ies = wpabuf_head(wps_ie);
  349. params.extra_ies_len = wpabuf_len(wps_ie);
  350. }
  351. }
  352. #endif /* CONFIG_WPS */
  353. params.filter_ssids = wpa_supplicant_build_filter_ssids(
  354. wpa_s->conf, &params.num_filter_ssids);
  355. ret = wpa_supplicant_trigger_scan(wpa_s, &params);
  356. wpabuf_free(wps_ie);
  357. os_free(params.freqs);
  358. os_free(params.filter_ssids);
  359. if (ret) {
  360. wpa_printf(MSG_WARNING, "Failed to initiate AP scan.");
  361. if (prev_state != wpa_s->wpa_state)
  362. wpa_supplicant_set_state(wpa_s, prev_state);
  363. wpa_supplicant_req_scan(wpa_s, 1, 0);
  364. } else
  365. wpa_s->scan_runs++;
  366. }
  367. /**
  368. * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
  369. * @wpa_s: Pointer to wpa_supplicant data
  370. * @sec: Number of seconds after which to scan
  371. * @usec: Number of microseconds after which to scan
  372. *
  373. * This function is used to schedule a scan for neighboring access points after
  374. * the specified time.
  375. */
  376. void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
  377. {
  378. /* If there's at least one network that should be specifically scanned
  379. * then don't cancel the scan and reschedule. Some drivers do
  380. * background scanning which generates frequent scan results, and that
  381. * causes the specific SSID scan to get continually pushed back and
  382. * never happen, which causes hidden APs to never get probe-scanned.
  383. */
  384. if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
  385. wpa_s->conf->ap_scan == 1) {
  386. struct wpa_ssid *ssid = wpa_s->conf->ssid;
  387. while (ssid) {
  388. if (!ssid->disabled && ssid->scan_ssid)
  389. break;
  390. ssid = ssid->next;
  391. }
  392. if (ssid) {
  393. wpa_msg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
  394. "ensure that specific SSID scans occur");
  395. return;
  396. }
  397. }
  398. wpa_msg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
  399. sec, usec);
  400. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  401. eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
  402. }
  403. /**
  404. * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
  405. * @wpa_s: Pointer to wpa_supplicant data
  406. *
  407. * This function is used to cancel a scan request scheduled with
  408. * wpa_supplicant_req_scan().
  409. */
  410. void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
  411. {
  412. wpa_msg(wpa_s, MSG_DEBUG, "Cancelling scan request");
  413. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  414. }
  415. void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
  416. int scanning)
  417. {
  418. if (wpa_s->scanning != scanning) {
  419. wpa_s->scanning = scanning;
  420. wpas_notify_scanning(wpa_s);
  421. }
  422. }
  423. static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
  424. {
  425. int rate = 0;
  426. const u8 *ie;
  427. int i;
  428. ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
  429. for (i = 0; ie && i < ie[1]; i++) {
  430. if ((ie[i + 2] & 0x7f) > rate)
  431. rate = ie[i + 2] & 0x7f;
  432. }
  433. ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
  434. for (i = 0; ie && i < ie[1]; i++) {
  435. if ((ie[i + 2] & 0x7f) > rate)
  436. rate = ie[i + 2] & 0x7f;
  437. }
  438. return rate;
  439. }
  440. const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
  441. {
  442. const u8 *end, *pos;
  443. pos = (const u8 *) (res + 1);
  444. end = pos + res->ie_len;
  445. while (pos + 1 < end) {
  446. if (pos + 2 + pos[1] > end)
  447. break;
  448. if (pos[0] == ie)
  449. return pos;
  450. pos += 2 + pos[1];
  451. }
  452. return NULL;
  453. }
  454. const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
  455. u32 vendor_type)
  456. {
  457. const u8 *end, *pos;
  458. pos = (const u8 *) (res + 1);
  459. end = pos + res->ie_len;
  460. while (pos + 1 < end) {
  461. if (pos + 2 + pos[1] > end)
  462. break;
  463. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  464. vendor_type == WPA_GET_BE32(&pos[2]))
  465. return pos;
  466. pos += 2 + pos[1];
  467. }
  468. return NULL;
  469. }
  470. struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
  471. u32 vendor_type)
  472. {
  473. struct wpabuf *buf;
  474. const u8 *end, *pos;
  475. buf = wpabuf_alloc(res->ie_len);
  476. if (buf == NULL)
  477. return NULL;
  478. pos = (const u8 *) (res + 1);
  479. end = pos + res->ie_len;
  480. while (pos + 1 < end) {
  481. if (pos + 2 + pos[1] > end)
  482. break;
  483. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  484. vendor_type == WPA_GET_BE32(&pos[2]))
  485. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  486. pos += 2 + pos[1];
  487. }
  488. if (wpabuf_len(buf) == 0) {
  489. wpabuf_free(buf);
  490. buf = NULL;
  491. }
  492. return buf;
  493. }
  494. /* Compare function for sorting scan results. Return >0 if @b is considered
  495. * better. */
  496. static int wpa_scan_result_compar(const void *a, const void *b)
  497. {
  498. struct wpa_scan_res **_wa = (void *) a;
  499. struct wpa_scan_res **_wb = (void *) b;
  500. struct wpa_scan_res *wa = *_wa;
  501. struct wpa_scan_res *wb = *_wb;
  502. int wpa_a, wpa_b, maxrate_a, maxrate_b;
  503. /* WPA/WPA2 support preferred */
  504. wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
  505. wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
  506. wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
  507. wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
  508. if (wpa_b && !wpa_a)
  509. return 1;
  510. if (!wpa_b && wpa_a)
  511. return -1;
  512. /* privacy support preferred */
  513. if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
  514. (wb->caps & IEEE80211_CAP_PRIVACY))
  515. return 1;
  516. if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
  517. (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
  518. return -1;
  519. /* best/max rate preferred if signal level close enough XXX */
  520. if ((wa->level && wb->level && abs(wb->level - wa->level) < 5) ||
  521. (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
  522. maxrate_a = wpa_scan_get_max_rate(wa);
  523. maxrate_b = wpa_scan_get_max_rate(wb);
  524. if (maxrate_a != maxrate_b)
  525. return maxrate_b - maxrate_a;
  526. }
  527. /* use freq for channel preference */
  528. /* all things being equal, use signal level; if signal levels are
  529. * identical, use quality values since some drivers may only report
  530. * that value and leave the signal level zero */
  531. if (wb->level == wa->level)
  532. return wb->qual - wa->qual;
  533. return wb->level - wa->level;
  534. }
  535. /**
  536. * wpa_supplicant_get_scan_results - Get scan results
  537. * @wpa_s: Pointer to wpa_supplicant data
  538. * @info: Information about what was scanned or %NULL if not available
  539. * @new_scan: Whether a new scan was performed
  540. * Returns: Scan results, %NULL on failure
  541. *
  542. * This function request the current scan results from the driver and updates
  543. * the local BSS list wpa_s->bss. The caller is responsible for freeing the
  544. * results with wpa_scan_results_free().
  545. */
  546. struct wpa_scan_results *
  547. wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
  548. struct scan_info *info, int new_scan)
  549. {
  550. struct wpa_scan_results *scan_res;
  551. size_t i;
  552. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  553. scan_res = ieee80211_sta_get_scan_results(wpa_s);
  554. else
  555. scan_res = wpa_drv_get_scan_results2(wpa_s);
  556. if (scan_res == NULL) {
  557. wpa_printf(MSG_DEBUG, "Failed to get scan results");
  558. return NULL;
  559. }
  560. qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
  561. wpa_scan_result_compar);
  562. wpa_bss_update_start(wpa_s);
  563. for (i = 0; i < scan_res->num; i++)
  564. wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
  565. wpa_bss_update_end(wpa_s, info, new_scan);
  566. return scan_res;
  567. }
  568. int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
  569. {
  570. struct wpa_scan_results *scan_res;
  571. scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
  572. if (scan_res == NULL)
  573. return -1;
  574. wpa_scan_results_free(scan_res);
  575. return 0;
  576. }
  577. void wpa_scan_results_free(struct wpa_scan_results *res)
  578. {
  579. size_t i;
  580. if (res == NULL)
  581. return;
  582. for (i = 0; i < res->num; i++)
  583. os_free(res->res[i]);
  584. os_free(res->res);
  585. os_free(res);
  586. }