scan.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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_P2P
  327. wpa_s->wps->dev.p2p = 1;
  328. if (!wps) {
  329. wps = 1;
  330. req_type = WPS_REQ_ENROLLEE_INFO;
  331. }
  332. if (params.freqs == NULL && wpa_s->p2p_in_provisioning &&
  333. wpa_s->go_params) {
  334. /* Optimize provisioning state scan based on GO information */
  335. if (wpa_s->p2p_in_provisioning < 5 &&
  336. wpa_s->go_params->freq > 0) {
  337. wpa_printf(MSG_DEBUG, "P2P: Scan only GO preferred "
  338. "frequency %d MHz",
  339. wpa_s->go_params->freq);
  340. params.freqs = os_zalloc(2 * sizeof(int));
  341. if (params.freqs)
  342. params.freqs[0] = wpa_s->go_params->freq;
  343. } else if (wpa_s->go_params->freq_list[0]) {
  344. wpa_printf(MSG_DEBUG, "P2P: Scan only common "
  345. "channels");
  346. int_array_concat(&params.freqs,
  347. wpa_s->go_params->freq_list);
  348. if (params.freqs)
  349. int_array_sort_unique(params.freqs);
  350. }
  351. wpa_s->p2p_in_provisioning++;
  352. }
  353. #endif /* CONFIG_P2P */
  354. #ifdef CONFIG_WPS
  355. if (params.freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
  356. /*
  357. * Optimize post-provisioning scan based on channel used
  358. * during provisioning.
  359. */
  360. wpa_printf(MSG_DEBUG, "WPS: Scan only frequency %u MHz that "
  361. "was used during provisioning", wpa_s->wps_freq);
  362. params.freqs = os_zalloc(2 * sizeof(int));
  363. if (params.freqs)
  364. params.freqs[0] = wpa_s->wps_freq;
  365. wpa_s->after_wps--;
  366. }
  367. if (wps) {
  368. wps_ie = wps_build_probe_req_ie(wps == 2, &wpa_s->wps->dev,
  369. wpa_s->wps->uuid, req_type);
  370. if (wps_ie) {
  371. params.extra_ies = wpabuf_head(wps_ie);
  372. params.extra_ies_len = wpabuf_len(wps_ie);
  373. }
  374. }
  375. #endif /* CONFIG_WPS */
  376. #ifdef CONFIG_P2P
  377. if (wps_ie) {
  378. if (wpabuf_resize(&wps_ie, 100) == 0) {
  379. wpas_p2p_scan_ie(wpa_s, wps_ie);
  380. params.extra_ies = wpabuf_head(wps_ie);
  381. params.extra_ies_len = wpabuf_len(wps_ie);
  382. }
  383. }
  384. #endif /* CONFIG_P2P */
  385. params.filter_ssids = wpa_supplicant_build_filter_ssids(
  386. wpa_s->conf, &params.num_filter_ssids);
  387. ret = wpa_supplicant_trigger_scan(wpa_s, &params);
  388. wpabuf_free(wps_ie);
  389. os_free(params.freqs);
  390. os_free(params.filter_ssids);
  391. if (ret) {
  392. wpa_printf(MSG_WARNING, "Failed to initiate AP scan.");
  393. if (prev_state != wpa_s->wpa_state)
  394. wpa_supplicant_set_state(wpa_s, prev_state);
  395. wpa_supplicant_req_scan(wpa_s, 1, 0);
  396. }
  397. }
  398. /**
  399. * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
  400. * @wpa_s: Pointer to wpa_supplicant data
  401. * @sec: Number of seconds after which to scan
  402. * @usec: Number of microseconds after which to scan
  403. *
  404. * This function is used to schedule a scan for neighboring access points after
  405. * the specified time.
  406. */
  407. void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
  408. {
  409. /* If there's at least one network that should be specifically scanned
  410. * then don't cancel the scan and reschedule. Some drivers do
  411. * background scanning which generates frequent scan results, and that
  412. * causes the specific SSID scan to get continually pushed back and
  413. * never happen, which causes hidden APs to never get probe-scanned.
  414. */
  415. if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
  416. wpa_s->conf->ap_scan == 1) {
  417. struct wpa_ssid *ssid = wpa_s->conf->ssid;
  418. while (ssid) {
  419. if (!ssid->disabled && ssid->scan_ssid)
  420. break;
  421. ssid = ssid->next;
  422. }
  423. if (ssid) {
  424. wpa_msg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
  425. "ensure that specific SSID scans occur");
  426. return;
  427. }
  428. }
  429. wpa_msg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
  430. sec, usec);
  431. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  432. eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
  433. }
  434. /**
  435. * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
  436. * @wpa_s: Pointer to wpa_supplicant data
  437. *
  438. * This function is used to cancel a scan request scheduled with
  439. * wpa_supplicant_req_scan().
  440. */
  441. void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
  442. {
  443. wpa_msg(wpa_s, MSG_DEBUG, "Cancelling scan request");
  444. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  445. }
  446. void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
  447. int scanning)
  448. {
  449. if (wpa_s->scanning != scanning) {
  450. wpa_s->scanning = scanning;
  451. wpas_notify_scanning(wpa_s);
  452. }
  453. }
  454. static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
  455. {
  456. int rate = 0;
  457. const u8 *ie;
  458. int i;
  459. ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
  460. for (i = 0; ie && i < ie[1]; i++) {
  461. if ((ie[i + 2] & 0x7f) > rate)
  462. rate = ie[i + 2] & 0x7f;
  463. }
  464. ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
  465. for (i = 0; ie && i < ie[1]; i++) {
  466. if ((ie[i + 2] & 0x7f) > rate)
  467. rate = ie[i + 2] & 0x7f;
  468. }
  469. return rate;
  470. }
  471. const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
  472. {
  473. const u8 *end, *pos;
  474. pos = (const u8 *) (res + 1);
  475. end = pos + res->ie_len;
  476. while (pos + 1 < end) {
  477. if (pos + 2 + pos[1] > end)
  478. break;
  479. if (pos[0] == ie)
  480. return pos;
  481. pos += 2 + pos[1];
  482. }
  483. return NULL;
  484. }
  485. const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
  486. u32 vendor_type)
  487. {
  488. const u8 *end, *pos;
  489. pos = (const u8 *) (res + 1);
  490. end = pos + res->ie_len;
  491. while (pos + 1 < end) {
  492. if (pos + 2 + pos[1] > end)
  493. break;
  494. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  495. vendor_type == WPA_GET_BE32(&pos[2]))
  496. return pos;
  497. pos += 2 + pos[1];
  498. }
  499. return NULL;
  500. }
  501. struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
  502. u32 vendor_type)
  503. {
  504. struct wpabuf *buf;
  505. const u8 *end, *pos;
  506. buf = wpabuf_alloc(res->ie_len);
  507. if (buf == NULL)
  508. return NULL;
  509. pos = (const u8 *) (res + 1);
  510. end = pos + res->ie_len;
  511. while (pos + 1 < end) {
  512. if (pos + 2 + pos[1] > end)
  513. break;
  514. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  515. vendor_type == WPA_GET_BE32(&pos[2]))
  516. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  517. pos += 2 + pos[1];
  518. }
  519. if (wpabuf_len(buf) == 0) {
  520. wpabuf_free(buf);
  521. buf = NULL;
  522. }
  523. return buf;
  524. }
  525. struct wpabuf * wpa_scan_get_vendor_ie_multi_beacon(
  526. const struct wpa_scan_res *res, u32 vendor_type)
  527. {
  528. struct wpabuf *buf;
  529. const u8 *end, *pos;
  530. if (res->beacon_ie_len == 0)
  531. return NULL;
  532. buf = wpabuf_alloc(res->beacon_ie_len);
  533. if (buf == NULL)
  534. return NULL;
  535. pos = (const u8 *) (res + 1);
  536. pos += res->ie_len;
  537. end = pos + res->beacon_ie_len;
  538. while (pos + 1 < end) {
  539. if (pos + 2 + pos[1] > end)
  540. break;
  541. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  542. vendor_type == WPA_GET_BE32(&pos[2]))
  543. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  544. pos += 2 + pos[1];
  545. }
  546. if (wpabuf_len(buf) == 0) {
  547. wpabuf_free(buf);
  548. buf = NULL;
  549. }
  550. return buf;
  551. }
  552. /* Compare function for sorting scan results. Return >0 if @b is considered
  553. * better. */
  554. static int wpa_scan_result_compar(const void *a, const void *b)
  555. {
  556. struct wpa_scan_res **_wa = (void *) a;
  557. struct wpa_scan_res **_wb = (void *) b;
  558. struct wpa_scan_res *wa = *_wa;
  559. struct wpa_scan_res *wb = *_wb;
  560. int wpa_a, wpa_b, maxrate_a, maxrate_b;
  561. /* WPA/WPA2 support preferred */
  562. wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
  563. wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
  564. wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
  565. wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
  566. if (wpa_b && !wpa_a)
  567. return 1;
  568. if (!wpa_b && wpa_a)
  569. return -1;
  570. /* privacy support preferred */
  571. if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
  572. (wb->caps & IEEE80211_CAP_PRIVACY))
  573. return 1;
  574. if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
  575. (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
  576. return -1;
  577. /* best/max rate preferred if signal level close enough XXX */
  578. if ((wa->level && wb->level && abs(wb->level - wa->level) < 5) ||
  579. (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
  580. maxrate_a = wpa_scan_get_max_rate(wa);
  581. maxrate_b = wpa_scan_get_max_rate(wb);
  582. if (maxrate_a != maxrate_b)
  583. return maxrate_b - maxrate_a;
  584. }
  585. /* use freq for channel preference */
  586. /* all things being equal, use signal level; if signal levels are
  587. * identical, use quality values since some drivers may only report
  588. * that value and leave the signal level zero */
  589. if (wb->level == wa->level)
  590. return wb->qual - wa->qual;
  591. return wb->level - wa->level;
  592. }
  593. #ifdef CONFIG_WPS
  594. /* Compare function for sorting scan results when searching a WPS AP for
  595. * provisioning. Return >0 if @b is considered better. */
  596. static int wpa_scan_result_wps_compar(const void *a, const void *b)
  597. {
  598. struct wpa_scan_res **_wa = (void *) a;
  599. struct wpa_scan_res **_wb = (void *) b;
  600. struct wpa_scan_res *wa = *_wa;
  601. struct wpa_scan_res *wb = *_wb;
  602. int uses_wps_a, uses_wps_b;
  603. struct wpabuf *wps_a, *wps_b;
  604. int res;
  605. /* Optimization - check WPS IE existence before allocated memory and
  606. * doing full reassembly. */
  607. uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
  608. uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
  609. if (uses_wps_a && !uses_wps_b)
  610. return -1;
  611. if (!uses_wps_a && uses_wps_b)
  612. return 1;
  613. if (uses_wps_a && uses_wps_b) {
  614. wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
  615. wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
  616. res = wps_ap_priority_compar(wps_a, wps_b);
  617. wpabuf_free(wps_a);
  618. wpabuf_free(wps_b);
  619. if (res)
  620. return res;
  621. }
  622. /*
  623. * Do not use current AP security policy as a sorting criteria during
  624. * WPS provisioning step since the AP may get reconfigured at the
  625. * completion of provisioning.
  626. */
  627. /* all things being equal, use signal level; if signal levels are
  628. * identical, use quality values since some drivers may only report
  629. * that value and leave the signal level zero */
  630. if (wb->level == wa->level)
  631. return wb->qual - wa->qual;
  632. return wb->level - wa->level;
  633. }
  634. #endif /* CONFIG_WPS */
  635. /**
  636. * wpa_supplicant_get_scan_results - Get scan results
  637. * @wpa_s: Pointer to wpa_supplicant data
  638. * @info: Information about what was scanned or %NULL if not available
  639. * @new_scan: Whether a new scan was performed
  640. * Returns: Scan results, %NULL on failure
  641. *
  642. * This function request the current scan results from the driver and updates
  643. * the local BSS list wpa_s->bss. The caller is responsible for freeing the
  644. * results with wpa_scan_results_free().
  645. */
  646. struct wpa_scan_results *
  647. wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
  648. struct scan_info *info, int new_scan)
  649. {
  650. struct wpa_scan_results *scan_res;
  651. size_t i;
  652. int (*compar)(const void *, const void *) = wpa_scan_result_compar;
  653. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  654. scan_res = ieee80211_sta_get_scan_results(wpa_s);
  655. else
  656. scan_res = wpa_drv_get_scan_results2(wpa_s);
  657. if (scan_res == NULL) {
  658. wpa_printf(MSG_DEBUG, "Failed to get scan results");
  659. return NULL;
  660. }
  661. #ifdef CONFIG_WPS
  662. if (wpas_wps_in_progress(wpa_s)) {
  663. wpa_printf(MSG_DEBUG, "WPS: Order scan results with WPS "
  664. "provisioning rules");
  665. compar = wpa_scan_result_wps_compar;
  666. }
  667. #endif /* CONFIG_WPS */
  668. qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
  669. compar);
  670. wpa_bss_update_start(wpa_s);
  671. for (i = 0; i < scan_res->num; i++)
  672. wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
  673. wpa_bss_update_end(wpa_s, info, new_scan);
  674. return scan_res;
  675. }
  676. int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
  677. {
  678. struct wpa_scan_results *scan_res;
  679. scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
  680. if (scan_res == NULL)
  681. return -1;
  682. wpa_scan_results_free(scan_res);
  683. return 0;
  684. }
  685. void wpa_scan_results_free(struct wpa_scan_results *res)
  686. {
  687. size_t i;
  688. if (res == NULL)
  689. return;
  690. for (i = 0; i < res->num; i++)
  691. os_free(res->res[i]);
  692. os_free(res->res);
  693. os_free(res);
  694. }