scan.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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->wpa_state == WPA_INTERFACE_DISABLED) {
  209. wpa_printf(MSG_DEBUG, "Skip scan - interface disabled");
  210. return;
  211. }
  212. if (wpa_s->disconnected && !wpa_s->scan_req) {
  213. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  214. return;
  215. }
  216. if (!wpa_supplicant_enabled_networks(wpa_s->conf) &&
  217. !wpa_s->scan_req) {
  218. wpa_printf(MSG_DEBUG, "No enabled networks - do not scan");
  219. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  220. return;
  221. }
  222. if (wpa_s->conf->ap_scan != 0 &&
  223. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
  224. wpa_printf(MSG_DEBUG, "Using wired authentication - "
  225. "overriding ap_scan configuration");
  226. wpa_s->conf->ap_scan = 0;
  227. wpas_notify_ap_scan_changed(wpa_s);
  228. }
  229. if (wpa_s->conf->ap_scan == 0) {
  230. wpa_supplicant_gen_assoc_event(wpa_s);
  231. return;
  232. }
  233. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
  234. wpa_s->conf->ap_scan == 2)
  235. max_ssids = 1;
  236. else {
  237. max_ssids = wpa_s->max_scan_ssids;
  238. if (max_ssids > WPAS_MAX_SCAN_SSIDS)
  239. max_ssids = WPAS_MAX_SCAN_SSIDS;
  240. }
  241. #ifdef CONFIG_WPS
  242. wps = wpas_wps_in_use(wpa_s->conf, &req_type);
  243. #endif /* CONFIG_WPS */
  244. if (wpa_s->scan_res_tried == 0 && wpa_s->conf->ap_scan == 1 &&
  245. !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) &&
  246. wps != 2 && !wpa_s->conf->filter_ssids &&
  247. !wpa_s->connect_without_scan) {
  248. wpa_s->scan_res_tried++;
  249. wpa_printf(MSG_DEBUG, "Trying to get current scan results "
  250. "first without requesting a new scan to speed up "
  251. "initial association");
  252. wpa_supplicant_event(wpa_s, EVENT_SCAN_RESULTS, NULL);
  253. return;
  254. }
  255. scan_req = wpa_s->scan_req;
  256. wpa_s->scan_req = 0;
  257. os_memset(&params, 0, sizeof(params));
  258. prev_state = wpa_s->wpa_state;
  259. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  260. wpa_s->wpa_state == WPA_INACTIVE)
  261. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  262. /* Find the starting point from which to continue scanning */
  263. ssid = wpa_s->conf->ssid;
  264. if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
  265. while (ssid) {
  266. if (ssid == wpa_s->prev_scan_ssid) {
  267. ssid = ssid->next;
  268. break;
  269. }
  270. ssid = ssid->next;
  271. }
  272. }
  273. if (scan_req != 2 && (wpa_s->conf->ap_scan == 2 ||
  274. wpa_s->connect_without_scan)) {
  275. wpa_s->connect_without_scan = 0;
  276. wpa_supplicant_assoc_try(wpa_s, ssid);
  277. return;
  278. } else if (wpa_s->conf->ap_scan == 2) {
  279. /*
  280. * User-initiated scan request in ap_scan == 2; scan with
  281. * wildcard SSID.
  282. */
  283. ssid = NULL;
  284. } else {
  285. struct wpa_ssid *start = ssid, *tssid;
  286. int freqs_set = 0;
  287. if (ssid == NULL && max_ssids > 1)
  288. ssid = wpa_s->conf->ssid;
  289. while (ssid) {
  290. if (!ssid->disabled && ssid->scan_ssid) {
  291. wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
  292. ssid->ssid, ssid->ssid_len);
  293. params.ssids[params.num_ssids].ssid =
  294. ssid->ssid;
  295. params.ssids[params.num_ssids].ssid_len =
  296. ssid->ssid_len;
  297. params.num_ssids++;
  298. if (params.num_ssids + 1 >= max_ssids)
  299. break;
  300. }
  301. ssid = ssid->next;
  302. if (ssid == start)
  303. break;
  304. if (ssid == NULL && max_ssids > 1 &&
  305. start != wpa_s->conf->ssid)
  306. ssid = wpa_s->conf->ssid;
  307. }
  308. for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
  309. if (tssid->disabled)
  310. continue;
  311. if ((params.freqs || !freqs_set) && tssid->scan_freq) {
  312. int_array_concat(&params.freqs,
  313. tssid->scan_freq);
  314. } else {
  315. os_free(params.freqs);
  316. params.freqs = NULL;
  317. }
  318. freqs_set = 1;
  319. }
  320. int_array_sort_unique(params.freqs);
  321. }
  322. if (ssid) {
  323. wpa_s->prev_scan_ssid = ssid;
  324. if (max_ssids > 1) {
  325. wpa_printf(MSG_DEBUG, "Include wildcard SSID in the "
  326. "scan request");
  327. params.num_ssids++;
  328. }
  329. wpa_printf(MSG_DEBUG, "Starting AP scan for specific SSID(s)");
  330. } else {
  331. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  332. params.num_ssids++;
  333. wpa_printf(MSG_DEBUG, "Starting AP scan for wildcard SSID");
  334. }
  335. #ifdef CONFIG_WPS
  336. if (params.freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
  337. /*
  338. * Optimize post-provisioning scan based on channel used
  339. * during provisioning.
  340. */
  341. wpa_printf(MSG_DEBUG, "WPS: Scan only frequency %u MHz that "
  342. "was used during provisioning", wpa_s->wps_freq);
  343. params.freqs = os_zalloc(2 * sizeof(int));
  344. if (params.freqs)
  345. params.freqs[0] = wpa_s->wps_freq;
  346. wpa_s->after_wps--;
  347. }
  348. if (wps) {
  349. wps_ie = wps_build_probe_req_ie(wps == 2, &wpa_s->wps->dev,
  350. wpa_s->wps->uuid, req_type);
  351. if (wps_ie) {
  352. params.extra_ies = wpabuf_head(wps_ie);
  353. params.extra_ies_len = wpabuf_len(wps_ie);
  354. }
  355. }
  356. #endif /* CONFIG_WPS */
  357. params.filter_ssids = wpa_supplicant_build_filter_ssids(
  358. wpa_s->conf, &params.num_filter_ssids);
  359. ret = wpa_supplicant_trigger_scan(wpa_s, &params);
  360. wpabuf_free(wps_ie);
  361. os_free(params.freqs);
  362. os_free(params.filter_ssids);
  363. if (ret) {
  364. wpa_printf(MSG_WARNING, "Failed to initiate AP scan.");
  365. if (prev_state != wpa_s->wpa_state)
  366. wpa_supplicant_set_state(wpa_s, prev_state);
  367. wpa_supplicant_req_scan(wpa_s, 1, 0);
  368. }
  369. }
  370. /**
  371. * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
  372. * @wpa_s: Pointer to wpa_supplicant data
  373. * @sec: Number of seconds after which to scan
  374. * @usec: Number of microseconds after which to scan
  375. *
  376. * This function is used to schedule a scan for neighboring access points after
  377. * the specified time.
  378. */
  379. void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
  380. {
  381. /* If there's at least one network that should be specifically scanned
  382. * then don't cancel the scan and reschedule. Some drivers do
  383. * background scanning which generates frequent scan results, and that
  384. * causes the specific SSID scan to get continually pushed back and
  385. * never happen, which causes hidden APs to never get probe-scanned.
  386. */
  387. if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
  388. wpa_s->conf->ap_scan == 1) {
  389. struct wpa_ssid *ssid = wpa_s->conf->ssid;
  390. while (ssid) {
  391. if (!ssid->disabled && ssid->scan_ssid)
  392. break;
  393. ssid = ssid->next;
  394. }
  395. if (ssid) {
  396. wpa_msg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
  397. "ensure that specific SSID scans occur");
  398. return;
  399. }
  400. }
  401. wpa_msg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
  402. sec, usec);
  403. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  404. eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
  405. }
  406. /**
  407. * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
  408. * @wpa_s: Pointer to wpa_supplicant data
  409. *
  410. * This function is used to cancel a scan request scheduled with
  411. * wpa_supplicant_req_scan().
  412. */
  413. void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
  414. {
  415. wpa_msg(wpa_s, MSG_DEBUG, "Cancelling scan request");
  416. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  417. }
  418. void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
  419. int scanning)
  420. {
  421. if (wpa_s->scanning != scanning) {
  422. wpa_s->scanning = scanning;
  423. wpas_notify_scanning(wpa_s);
  424. }
  425. }
  426. static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
  427. {
  428. int rate = 0;
  429. const u8 *ie;
  430. int i;
  431. ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
  432. for (i = 0; ie && i < ie[1]; i++) {
  433. if ((ie[i + 2] & 0x7f) > rate)
  434. rate = ie[i + 2] & 0x7f;
  435. }
  436. ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
  437. for (i = 0; ie && i < ie[1]; i++) {
  438. if ((ie[i + 2] & 0x7f) > rate)
  439. rate = ie[i + 2] & 0x7f;
  440. }
  441. return rate;
  442. }
  443. const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
  444. {
  445. const u8 *end, *pos;
  446. pos = (const u8 *) (res + 1);
  447. end = pos + res->ie_len;
  448. while (pos + 1 < end) {
  449. if (pos + 2 + pos[1] > end)
  450. break;
  451. if (pos[0] == ie)
  452. return pos;
  453. pos += 2 + pos[1];
  454. }
  455. return NULL;
  456. }
  457. const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
  458. u32 vendor_type)
  459. {
  460. const u8 *end, *pos;
  461. pos = (const u8 *) (res + 1);
  462. end = pos + res->ie_len;
  463. while (pos + 1 < end) {
  464. if (pos + 2 + pos[1] > end)
  465. break;
  466. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  467. vendor_type == WPA_GET_BE32(&pos[2]))
  468. return pos;
  469. pos += 2 + pos[1];
  470. }
  471. return NULL;
  472. }
  473. struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
  474. u32 vendor_type)
  475. {
  476. struct wpabuf *buf;
  477. const u8 *end, *pos;
  478. buf = wpabuf_alloc(res->ie_len);
  479. if (buf == NULL)
  480. return NULL;
  481. pos = (const u8 *) (res + 1);
  482. end = pos + res->ie_len;
  483. while (pos + 1 < end) {
  484. if (pos + 2 + pos[1] > end)
  485. break;
  486. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  487. vendor_type == WPA_GET_BE32(&pos[2]))
  488. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  489. pos += 2 + pos[1];
  490. }
  491. if (wpabuf_len(buf) == 0) {
  492. wpabuf_free(buf);
  493. buf = NULL;
  494. }
  495. return buf;
  496. }
  497. /* Compare function for sorting scan results. Return >0 if @b is considered
  498. * better. */
  499. static int wpa_scan_result_compar(const void *a, const void *b)
  500. {
  501. struct wpa_scan_res **_wa = (void *) a;
  502. struct wpa_scan_res **_wb = (void *) b;
  503. struct wpa_scan_res *wa = *_wa;
  504. struct wpa_scan_res *wb = *_wb;
  505. int wpa_a, wpa_b, maxrate_a, maxrate_b;
  506. /* WPA/WPA2 support preferred */
  507. wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
  508. wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
  509. wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
  510. wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
  511. if (wpa_b && !wpa_a)
  512. return 1;
  513. if (!wpa_b && wpa_a)
  514. return -1;
  515. /* privacy support preferred */
  516. if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
  517. (wb->caps & IEEE80211_CAP_PRIVACY))
  518. return 1;
  519. if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
  520. (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
  521. return -1;
  522. /* best/max rate preferred if signal level close enough XXX */
  523. if ((wa->level && wb->level && abs(wb->level - wa->level) < 5) ||
  524. (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
  525. maxrate_a = wpa_scan_get_max_rate(wa);
  526. maxrate_b = wpa_scan_get_max_rate(wb);
  527. if (maxrate_a != maxrate_b)
  528. return maxrate_b - maxrate_a;
  529. }
  530. /* use freq for channel preference */
  531. /* all things being equal, use signal level; if signal levels are
  532. * identical, use quality values since some drivers may only report
  533. * that value and leave the signal level zero */
  534. if (wb->level == wa->level)
  535. return wb->qual - wa->qual;
  536. return wb->level - wa->level;
  537. }
  538. #ifdef CONFIG_WPS
  539. /* Compare function for sorting scan results when searching a WPS AP for
  540. * provisioning. Return >0 if @b is considered better. */
  541. static int wpa_scan_result_wps_compar(const void *a, const void *b)
  542. {
  543. struct wpa_scan_res **_wa = (void *) a;
  544. struct wpa_scan_res **_wb = (void *) b;
  545. struct wpa_scan_res *wa = *_wa;
  546. struct wpa_scan_res *wb = *_wb;
  547. int uses_wps_a, uses_wps_b;
  548. struct wpabuf *wps_a, *wps_b;
  549. int res;
  550. /* Optimization - check WPS IE existence before allocated memory and
  551. * doing full reassembly. */
  552. uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
  553. uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
  554. if (uses_wps_a && !uses_wps_b)
  555. return -1;
  556. if (!uses_wps_a && uses_wps_b)
  557. return 1;
  558. if (uses_wps_a && uses_wps_b) {
  559. wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
  560. wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
  561. res = wps_ap_priority_compar(wps_a, wps_b);
  562. wpabuf_free(wps_a);
  563. wpabuf_free(wps_b);
  564. if (res)
  565. return res;
  566. }
  567. /*
  568. * Do not use current AP security policy as a sorting criteria during
  569. * WPS provisioning step since the AP may get reconfigured at the
  570. * completion of provisioning.
  571. */
  572. /* all things being equal, use signal level; if signal levels are
  573. * identical, use quality values since some drivers may only report
  574. * that value and leave the signal level zero */
  575. if (wb->level == wa->level)
  576. return wb->qual - wa->qual;
  577. return wb->level - wa->level;
  578. }
  579. #endif /* CONFIG_WPS */
  580. /**
  581. * wpa_supplicant_get_scan_results - Get scan results
  582. * @wpa_s: Pointer to wpa_supplicant data
  583. * @info: Information about what was scanned or %NULL if not available
  584. * @new_scan: Whether a new scan was performed
  585. * Returns: Scan results, %NULL on failure
  586. *
  587. * This function request the current scan results from the driver and updates
  588. * the local BSS list wpa_s->bss. The caller is responsible for freeing the
  589. * results with wpa_scan_results_free().
  590. */
  591. struct wpa_scan_results *
  592. wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
  593. struct scan_info *info, int new_scan)
  594. {
  595. struct wpa_scan_results *scan_res;
  596. size_t i;
  597. int (*compar)(const void *, const void *) = wpa_scan_result_compar;
  598. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  599. scan_res = ieee80211_sta_get_scan_results(wpa_s);
  600. else
  601. scan_res = wpa_drv_get_scan_results2(wpa_s);
  602. if (scan_res == NULL) {
  603. wpa_printf(MSG_DEBUG, "Failed to get scan results");
  604. return NULL;
  605. }
  606. #ifdef CONFIG_WPS
  607. if (wpas_wps_in_progress(wpa_s)) {
  608. wpa_printf(MSG_DEBUG, "WPS: Order scan results with WPS "
  609. "provisioning rules");
  610. compar = wpa_scan_result_wps_compar;
  611. }
  612. #endif /* CONFIG_WPS */
  613. qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
  614. compar);
  615. wpa_bss_update_start(wpa_s);
  616. for (i = 0; i < scan_res->num; i++)
  617. wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
  618. wpa_bss_update_end(wpa_s, info, new_scan);
  619. return scan_res;
  620. }
  621. int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
  622. {
  623. struct wpa_scan_results *scan_res;
  624. scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
  625. if (scan_res == NULL)
  626. return -1;
  627. wpa_scan_results_free(scan_res);
  628. return 0;
  629. }
  630. void wpa_scan_results_free(struct wpa_scan_results *res)
  631. {
  632. size_t i;
  633. if (res == NULL)
  634. return;
  635. for (i = 0; i < res->num; i++)
  636. os_free(res->res[i]);
  637. os_free(res->res);
  638. os_free(res);
  639. }