scan.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  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 "p2p_supplicant.h"
  24. #include "p2p/p2p.h"
  25. #include "notify.h"
  26. #include "bss.h"
  27. #include "scan.h"
  28. static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
  29. {
  30. struct wpa_ssid *ssid;
  31. union wpa_event_data data;
  32. ssid = wpa_supplicant_get_ssid(wpa_s);
  33. if (ssid == NULL)
  34. return;
  35. if (wpa_s->current_ssid == NULL) {
  36. wpa_s->current_ssid = ssid;
  37. if (wpa_s->current_ssid != NULL)
  38. wpas_notify_network_changed(wpa_s);
  39. }
  40. wpa_supplicant_initiate_eapol(wpa_s);
  41. wpa_printf(MSG_DEBUG, "Already associated with a configured network - "
  42. "generating associated event");
  43. os_memset(&data, 0, sizeof(data));
  44. wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
  45. }
  46. #ifdef CONFIG_WPS
  47. static int wpas_wps_in_use(struct wpa_config *conf,
  48. enum wps_request_type *req_type)
  49. {
  50. struct wpa_ssid *ssid;
  51. int wps = 0;
  52. for (ssid = conf->ssid; ssid; ssid = ssid->next) {
  53. if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
  54. continue;
  55. wps = 1;
  56. *req_type = wpas_wps_get_req_type(ssid);
  57. if (!ssid->eap.phase1)
  58. continue;
  59. if (os_strstr(ssid->eap.phase1, "pbc=1"))
  60. return 2;
  61. }
  62. return wps;
  63. }
  64. #endif /* CONFIG_WPS */
  65. int wpa_supplicant_enabled_networks(struct wpa_config *conf)
  66. {
  67. struct wpa_ssid *ssid = conf->ssid;
  68. while (ssid) {
  69. if (!ssid->disabled)
  70. return 1;
  71. ssid = ssid->next;
  72. }
  73. return 0;
  74. }
  75. static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
  76. struct wpa_ssid *ssid)
  77. {
  78. while (ssid) {
  79. if (!ssid->disabled)
  80. break;
  81. ssid = ssid->next;
  82. }
  83. /* ap_scan=2 mode - try to associate with each SSID. */
  84. if (ssid == NULL) {
  85. wpa_printf(MSG_DEBUG, "wpa_supplicant_scan: Reached "
  86. "end of scan list - go back to beginning");
  87. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  88. wpa_supplicant_req_scan(wpa_s, 0, 0);
  89. return;
  90. }
  91. if (ssid->next) {
  92. /* Continue from the next SSID on the next attempt. */
  93. wpa_s->prev_scan_ssid = ssid;
  94. } else {
  95. /* Start from the beginning of the SSID list. */
  96. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  97. }
  98. wpa_supplicant_associate(wpa_s, NULL, ssid);
  99. }
  100. static int int_array_len(const int *a)
  101. {
  102. int i;
  103. for (i = 0; a && a[i]; i++)
  104. ;
  105. return i;
  106. }
  107. static void int_array_concat(int **res, const int *a)
  108. {
  109. int reslen, alen, i;
  110. int *n;
  111. reslen = int_array_len(*res);
  112. alen = int_array_len(a);
  113. n = os_realloc(*res, (reslen + alen + 1) * sizeof(int));
  114. if (n == NULL) {
  115. os_free(*res);
  116. *res = NULL;
  117. return;
  118. }
  119. for (i = 0; i <= alen; i++)
  120. n[reslen + i] = a[i];
  121. *res = n;
  122. }
  123. static int freq_cmp(const void *a, const void *b)
  124. {
  125. int _a = *(int *) a;
  126. int _b = *(int *) b;
  127. if (_a == 0)
  128. return 1;
  129. if (_b == 0)
  130. return -1;
  131. return _a - _b;
  132. }
  133. static void int_array_sort_unique(int *a)
  134. {
  135. int alen;
  136. int i, j;
  137. if (a == NULL)
  138. return;
  139. alen = int_array_len(a);
  140. qsort(a, alen, sizeof(int), freq_cmp);
  141. i = 0;
  142. j = 1;
  143. while (a[i] && a[j]) {
  144. if (a[i] == a[j]) {
  145. j++;
  146. continue;
  147. }
  148. a[++i] = a[j++];
  149. }
  150. if (a[i])
  151. i++;
  152. a[i] = 0;
  153. }
  154. int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
  155. struct wpa_driver_scan_params *params)
  156. {
  157. int ret;
  158. wpa_supplicant_notify_scanning(wpa_s, 1);
  159. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  160. ret = ieee80211_sta_req_scan(wpa_s, params);
  161. else
  162. ret = wpa_drv_scan(wpa_s, params);
  163. if (ret) {
  164. wpa_supplicant_notify_scanning(wpa_s, 0);
  165. wpas_notify_scan_done(wpa_s, 0);
  166. } else
  167. wpa_s->scan_runs++;
  168. return ret;
  169. }
  170. static struct wpa_driver_scan_filter *
  171. wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
  172. {
  173. struct wpa_driver_scan_filter *ssids;
  174. struct wpa_ssid *ssid;
  175. size_t count;
  176. *num_ssids = 0;
  177. if (!conf->filter_ssids)
  178. return NULL;
  179. for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
  180. if (ssid->ssid && ssid->ssid_len)
  181. count++;
  182. }
  183. if (count == 0)
  184. return NULL;
  185. ssids = os_zalloc(count * sizeof(struct wpa_driver_scan_filter));
  186. if (ssids == NULL)
  187. return NULL;
  188. for (ssid = conf->ssid; ssid; ssid = ssid->next) {
  189. if (!ssid->ssid || !ssid->ssid_len)
  190. continue;
  191. os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
  192. ssids[*num_ssids].ssid_len = ssid->ssid_len;
  193. (*num_ssids)++;
  194. }
  195. return ssids;
  196. }
  197. static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
  198. {
  199. struct wpa_supplicant *wpa_s = eloop_ctx;
  200. struct wpa_ssid *ssid;
  201. int scan_req = 0, ret;
  202. struct wpabuf *wps_ie = NULL;
  203. #ifdef CONFIG_WPS
  204. int wps = 0;
  205. enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
  206. #endif /* CONFIG_WPS */
  207. struct wpa_driver_scan_params params;
  208. size_t max_ssids;
  209. enum wpa_states prev_state;
  210. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  211. wpa_printf(MSG_DEBUG, "Skip scan - interface disabled");
  212. return;
  213. }
  214. if (wpa_s->disconnected && !wpa_s->scan_req) {
  215. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  216. return;
  217. }
  218. if (!wpa_supplicant_enabled_networks(wpa_s->conf) &&
  219. !wpa_s->scan_req) {
  220. wpa_printf(MSG_DEBUG, "No enabled networks - do not scan");
  221. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  222. return;
  223. }
  224. if (wpa_s->conf->ap_scan != 0 &&
  225. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
  226. wpa_printf(MSG_DEBUG, "Using wired authentication - "
  227. "overriding ap_scan configuration");
  228. wpa_s->conf->ap_scan = 0;
  229. wpas_notify_ap_scan_changed(wpa_s);
  230. }
  231. if (wpa_s->conf->ap_scan == 0) {
  232. wpa_supplicant_gen_assoc_event(wpa_s);
  233. return;
  234. }
  235. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
  236. wpa_s->conf->ap_scan == 2)
  237. max_ssids = 1;
  238. else {
  239. max_ssids = wpa_s->max_scan_ssids;
  240. if (max_ssids > WPAS_MAX_SCAN_SSIDS)
  241. max_ssids = WPAS_MAX_SCAN_SSIDS;
  242. }
  243. #ifdef CONFIG_WPS
  244. wps = wpas_wps_in_use(wpa_s->conf, &req_type);
  245. #endif /* CONFIG_WPS */
  246. scan_req = wpa_s->scan_req;
  247. wpa_s->scan_req = 0;
  248. os_memset(&params, 0, sizeof(params));
  249. prev_state = wpa_s->wpa_state;
  250. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  251. wpa_s->wpa_state == WPA_INACTIVE)
  252. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  253. /* Find the starting point from which to continue scanning */
  254. ssid = wpa_s->conf->ssid;
  255. if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
  256. while (ssid) {
  257. if (ssid == wpa_s->prev_scan_ssid) {
  258. ssid = ssid->next;
  259. break;
  260. }
  261. ssid = ssid->next;
  262. }
  263. }
  264. if (scan_req != 2 && (wpa_s->conf->ap_scan == 2 ||
  265. wpa_s->connect_without_scan)) {
  266. wpa_s->connect_without_scan = 0;
  267. wpa_supplicant_assoc_try(wpa_s, ssid);
  268. return;
  269. } else if (wpa_s->conf->ap_scan == 2) {
  270. /*
  271. * User-initiated scan request in ap_scan == 2; scan with
  272. * wildcard SSID.
  273. */
  274. ssid = NULL;
  275. } else {
  276. struct wpa_ssid *start = ssid, *tssid;
  277. int freqs_set = 0;
  278. if (ssid == NULL && max_ssids > 1)
  279. ssid = wpa_s->conf->ssid;
  280. while (ssid) {
  281. if (!ssid->disabled && ssid->scan_ssid) {
  282. wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
  283. ssid->ssid, ssid->ssid_len);
  284. params.ssids[params.num_ssids].ssid =
  285. ssid->ssid;
  286. params.ssids[params.num_ssids].ssid_len =
  287. ssid->ssid_len;
  288. params.num_ssids++;
  289. if (params.num_ssids + 1 >= max_ssids)
  290. break;
  291. }
  292. ssid = ssid->next;
  293. if (ssid == start)
  294. break;
  295. if (ssid == NULL && max_ssids > 1 &&
  296. start != wpa_s->conf->ssid)
  297. ssid = wpa_s->conf->ssid;
  298. }
  299. for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
  300. if (tssid->disabled)
  301. continue;
  302. if ((params.freqs || !freqs_set) && tssid->scan_freq) {
  303. int_array_concat(&params.freqs,
  304. tssid->scan_freq);
  305. } else {
  306. os_free(params.freqs);
  307. params.freqs = NULL;
  308. }
  309. freqs_set = 1;
  310. }
  311. int_array_sort_unique(params.freqs);
  312. }
  313. if (ssid) {
  314. wpa_s->prev_scan_ssid = ssid;
  315. if (max_ssids > 1) {
  316. wpa_printf(MSG_DEBUG, "Include wildcard SSID in the "
  317. "scan request");
  318. params.num_ssids++;
  319. }
  320. wpa_printf(MSG_DEBUG, "Starting AP scan for specific SSID(s)");
  321. } else {
  322. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  323. params.num_ssids++;
  324. wpa_printf(MSG_DEBUG, "Starting AP scan for wildcard SSID");
  325. }
  326. #ifdef CONFIG_WPS
  327. if (params.freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
  328. /*
  329. * Optimize post-provisioning scan based on channel used
  330. * during provisioning.
  331. */
  332. wpa_printf(MSG_DEBUG, "WPS: Scan only frequency %u MHz that "
  333. "was used during provisioning", wpa_s->wps_freq);
  334. params.freqs = os_zalloc(2 * sizeof(int));
  335. if (params.freqs)
  336. params.freqs[0] = wpa_s->wps_freq;
  337. wpa_s->after_wps--;
  338. }
  339. if (wps) {
  340. wps_ie = wps_build_probe_req_ie(wps == 2, &wpa_s->wps->dev,
  341. wpa_s->wps->uuid, req_type);
  342. if (wps_ie) {
  343. params.extra_ies = wpabuf_head(wps_ie);
  344. params.extra_ies_len = wpabuf_len(wps_ie);
  345. }
  346. }
  347. #endif /* CONFIG_WPS */
  348. #ifdef CONFIG_P2P
  349. if (wps_ie) {
  350. if (wpabuf_resize(&wps_ie, 100) == 0) {
  351. wpas_p2p_scan_ie(wpa_s, wps_ie);
  352. params.extra_ies = wpabuf_head(wps_ie);
  353. params.extra_ies_len = wpabuf_len(wps_ie);
  354. }
  355. }
  356. #endif /* CONFIG_P2P */
  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. struct wpabuf * wpa_scan_get_vendor_ie_multi_beacon(
  498. const struct wpa_scan_res *res, u32 vendor_type)
  499. {
  500. struct wpabuf *buf;
  501. const u8 *end, *pos;
  502. if (res->beacon_ie_len == 0)
  503. return NULL;
  504. buf = wpabuf_alloc(res->beacon_ie_len);
  505. if (buf == NULL)
  506. return NULL;
  507. pos = (const u8 *) (res + 1);
  508. pos += res->ie_len;
  509. end = pos + res->beacon_ie_len;
  510. while (pos + 1 < end) {
  511. if (pos + 2 + pos[1] > end)
  512. break;
  513. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  514. vendor_type == WPA_GET_BE32(&pos[2]))
  515. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  516. pos += 2 + pos[1];
  517. }
  518. if (wpabuf_len(buf) == 0) {
  519. wpabuf_free(buf);
  520. buf = NULL;
  521. }
  522. return buf;
  523. }
  524. /* Compare function for sorting scan results. Return >0 if @b is considered
  525. * better. */
  526. static int wpa_scan_result_compar(const void *a, const void *b)
  527. {
  528. struct wpa_scan_res **_wa = (void *) a;
  529. struct wpa_scan_res **_wb = (void *) b;
  530. struct wpa_scan_res *wa = *_wa;
  531. struct wpa_scan_res *wb = *_wb;
  532. int wpa_a, wpa_b, maxrate_a, maxrate_b;
  533. /* WPA/WPA2 support preferred */
  534. wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
  535. wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
  536. wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
  537. wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
  538. if (wpa_b && !wpa_a)
  539. return 1;
  540. if (!wpa_b && wpa_a)
  541. return -1;
  542. /* privacy support preferred */
  543. if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
  544. (wb->caps & IEEE80211_CAP_PRIVACY))
  545. return 1;
  546. if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
  547. (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
  548. return -1;
  549. /* best/max rate preferred if signal level close enough XXX */
  550. if ((wa->level && wb->level && abs(wb->level - wa->level) < 5) ||
  551. (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
  552. maxrate_a = wpa_scan_get_max_rate(wa);
  553. maxrate_b = wpa_scan_get_max_rate(wb);
  554. if (maxrate_a != maxrate_b)
  555. return maxrate_b - maxrate_a;
  556. }
  557. /* use freq for channel preference */
  558. /* all things being equal, use signal level; if signal levels are
  559. * identical, use quality values since some drivers may only report
  560. * that value and leave the signal level zero */
  561. if (wb->level == wa->level)
  562. return wb->qual - wa->qual;
  563. return wb->level - wa->level;
  564. }
  565. #ifdef CONFIG_WPS
  566. /* Compare function for sorting scan results when searching a WPS AP for
  567. * provisioning. Return >0 if @b is considered better. */
  568. static int wpa_scan_result_wps_compar(const void *a, const void *b)
  569. {
  570. struct wpa_scan_res **_wa = (void *) a;
  571. struct wpa_scan_res **_wb = (void *) b;
  572. struct wpa_scan_res *wa = *_wa;
  573. struct wpa_scan_res *wb = *_wb;
  574. int uses_wps_a, uses_wps_b;
  575. struct wpabuf *wps_a, *wps_b;
  576. int res;
  577. /* Optimization - check WPS IE existence before allocated memory and
  578. * doing full reassembly. */
  579. uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
  580. uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
  581. if (uses_wps_a && !uses_wps_b)
  582. return -1;
  583. if (!uses_wps_a && uses_wps_b)
  584. return 1;
  585. if (uses_wps_a && uses_wps_b) {
  586. wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
  587. wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
  588. res = wps_ap_priority_compar(wps_a, wps_b);
  589. wpabuf_free(wps_a);
  590. wpabuf_free(wps_b);
  591. if (res)
  592. return res;
  593. }
  594. /*
  595. * Do not use current AP security policy as a sorting criteria during
  596. * WPS provisioning step since the AP may get reconfigured at the
  597. * completion of provisioning.
  598. */
  599. /* all things being equal, use signal level; if signal levels are
  600. * identical, use quality values since some drivers may only report
  601. * that value and leave the signal level zero */
  602. if (wb->level == wa->level)
  603. return wb->qual - wa->qual;
  604. return wb->level - wa->level;
  605. }
  606. #endif /* CONFIG_WPS */
  607. /**
  608. * wpa_supplicant_get_scan_results - Get scan results
  609. * @wpa_s: Pointer to wpa_supplicant data
  610. * @info: Information about what was scanned or %NULL if not available
  611. * @new_scan: Whether a new scan was performed
  612. * Returns: Scan results, %NULL on failure
  613. *
  614. * This function request the current scan results from the driver and updates
  615. * the local BSS list wpa_s->bss. The caller is responsible for freeing the
  616. * results with wpa_scan_results_free().
  617. */
  618. struct wpa_scan_results *
  619. wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
  620. struct scan_info *info, int new_scan)
  621. {
  622. struct wpa_scan_results *scan_res;
  623. size_t i;
  624. int (*compar)(const void *, const void *) = wpa_scan_result_compar;
  625. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  626. scan_res = ieee80211_sta_get_scan_results(wpa_s);
  627. else
  628. scan_res = wpa_drv_get_scan_results2(wpa_s);
  629. if (scan_res == NULL) {
  630. wpa_printf(MSG_DEBUG, "Failed to get scan results");
  631. return NULL;
  632. }
  633. #ifdef CONFIG_WPS
  634. if (wpas_wps_in_progress(wpa_s)) {
  635. wpa_printf(MSG_DEBUG, "WPS: Order scan results with WPS "
  636. "provisioning rules");
  637. compar = wpa_scan_result_wps_compar;
  638. }
  639. #endif /* CONFIG_WPS */
  640. qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
  641. compar);
  642. wpa_bss_update_start(wpa_s);
  643. for (i = 0; i < scan_res->num; i++)
  644. wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
  645. wpa_bss_update_end(wpa_s, info, new_scan);
  646. return scan_res;
  647. }
  648. int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
  649. {
  650. struct wpa_scan_results *scan_res;
  651. scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
  652. if (scan_res == NULL)
  653. return -1;
  654. wpa_scan_results_free(scan_res);
  655. return 0;
  656. }
  657. void wpa_scan_results_free(struct wpa_scan_results *res)
  658. {
  659. size_t i;
  660. if (res == NULL)
  661. return;
  662. for (i = 0; i < res->num; i++)
  663. os_free(res->res[i]);
  664. os_free(res->res);
  665. os_free(res);
  666. }