scan.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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. int count = 0;
  69. while (ssid) {
  70. if (!ssid->disabled)
  71. count++;
  72. ssid = ssid->next;
  73. }
  74. return count;
  75. }
  76. static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
  77. struct wpa_ssid *ssid)
  78. {
  79. while (ssid) {
  80. if (!ssid->disabled)
  81. break;
  82. ssid = ssid->next;
  83. }
  84. /* ap_scan=2 mode - try to associate with each SSID. */
  85. if (ssid == NULL) {
  86. wpa_printf(MSG_DEBUG, "wpa_supplicant_scan: Reached "
  87. "end of scan list - go back to beginning");
  88. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  89. wpa_supplicant_req_scan(wpa_s, 0, 0);
  90. return;
  91. }
  92. if (ssid->next) {
  93. /* Continue from the next SSID on the next attempt. */
  94. wpa_s->prev_scan_ssid = ssid;
  95. } else {
  96. /* Start from the beginning of the SSID list. */
  97. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  98. }
  99. wpa_supplicant_associate(wpa_s, NULL, ssid);
  100. }
  101. static int int_array_len(const int *a)
  102. {
  103. int i;
  104. for (i = 0; a && a[i]; i++)
  105. ;
  106. return i;
  107. }
  108. static void int_array_concat(int **res, const int *a)
  109. {
  110. int reslen, alen, i;
  111. int *n;
  112. reslen = int_array_len(*res);
  113. alen = int_array_len(a);
  114. n = os_realloc(*res, (reslen + alen + 1) * sizeof(int));
  115. if (n == NULL) {
  116. os_free(*res);
  117. *res = NULL;
  118. return;
  119. }
  120. for (i = 0; i <= alen; i++)
  121. n[reslen + i] = a[i];
  122. *res = n;
  123. }
  124. static int freq_cmp(const void *a, const void *b)
  125. {
  126. int _a = *(int *) a;
  127. int _b = *(int *) b;
  128. if (_a == 0)
  129. return 1;
  130. if (_b == 0)
  131. return -1;
  132. return _a - _b;
  133. }
  134. static void int_array_sort_unique(int *a)
  135. {
  136. int alen;
  137. int i, j;
  138. if (a == NULL)
  139. return;
  140. alen = int_array_len(a);
  141. qsort(a, alen, sizeof(int), freq_cmp);
  142. i = 0;
  143. j = 1;
  144. while (a[i] && a[j]) {
  145. if (a[i] == a[j]) {
  146. j++;
  147. continue;
  148. }
  149. a[++i] = a[j++];
  150. }
  151. if (a[i])
  152. i++;
  153. a[i] = 0;
  154. }
  155. int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
  156. struct wpa_driver_scan_params *params)
  157. {
  158. int ret;
  159. wpa_supplicant_notify_scanning(wpa_s, 1);
  160. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  161. ret = ieee80211_sta_req_scan(wpa_s, params);
  162. else
  163. ret = wpa_drv_scan(wpa_s, params);
  164. if (ret) {
  165. wpa_supplicant_notify_scanning(wpa_s, 0);
  166. wpas_notify_scan_done(wpa_s, 0);
  167. } else
  168. wpa_s->scan_runs++;
  169. return ret;
  170. }
  171. static struct wpa_driver_scan_filter *
  172. wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
  173. {
  174. struct wpa_driver_scan_filter *ssids;
  175. struct wpa_ssid *ssid;
  176. size_t count;
  177. *num_ssids = 0;
  178. if (!conf->filter_ssids)
  179. return NULL;
  180. for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
  181. if (ssid->ssid && ssid->ssid_len)
  182. count++;
  183. }
  184. if (count == 0)
  185. return NULL;
  186. ssids = os_zalloc(count * sizeof(struct wpa_driver_scan_filter));
  187. if (ssids == NULL)
  188. return NULL;
  189. for (ssid = conf->ssid; ssid; ssid = ssid->next) {
  190. if (!ssid->ssid || !ssid->ssid_len)
  191. continue;
  192. os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
  193. ssids[*num_ssids].ssid_len = ssid->ssid_len;
  194. (*num_ssids)++;
  195. }
  196. return ssids;
  197. }
  198. static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
  199. {
  200. struct wpa_supplicant *wpa_s = eloop_ctx;
  201. struct wpa_ssid *ssid;
  202. int scan_req = 0, ret;
  203. struct wpabuf *wps_ie = NULL;
  204. #ifdef CONFIG_WPS
  205. int wps = 0;
  206. enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
  207. #endif /* CONFIG_WPS */
  208. struct wpa_driver_scan_params params;
  209. size_t max_ssids;
  210. enum wpa_states prev_state;
  211. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  212. wpa_printf(MSG_DEBUG, "Skip scan - interface disabled");
  213. return;
  214. }
  215. if (wpa_s->disconnected && !wpa_s->scan_req) {
  216. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  217. return;
  218. }
  219. if (!wpa_supplicant_enabled_networks(wpa_s->conf) &&
  220. !wpa_s->scan_req) {
  221. wpa_printf(MSG_DEBUG, "No enabled networks - do not scan");
  222. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  223. return;
  224. }
  225. if (wpa_s->conf->ap_scan != 0 &&
  226. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
  227. wpa_printf(MSG_DEBUG, "Using wired authentication - "
  228. "overriding ap_scan configuration");
  229. wpa_s->conf->ap_scan = 0;
  230. wpas_notify_ap_scan_changed(wpa_s);
  231. }
  232. if (wpa_s->conf->ap_scan == 0) {
  233. wpa_supplicant_gen_assoc_event(wpa_s);
  234. return;
  235. }
  236. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
  237. wpa_s->conf->ap_scan == 2)
  238. max_ssids = 1;
  239. else {
  240. max_ssids = wpa_s->max_scan_ssids;
  241. if (max_ssids > WPAS_MAX_SCAN_SSIDS)
  242. max_ssids = WPAS_MAX_SCAN_SSIDS;
  243. }
  244. #ifdef CONFIG_WPS
  245. wps = wpas_wps_in_use(wpa_s->conf, &req_type);
  246. #endif /* CONFIG_WPS */
  247. scan_req = wpa_s->scan_req;
  248. wpa_s->scan_req = 0;
  249. os_memset(&params, 0, sizeof(params));
  250. prev_state = wpa_s->wpa_state;
  251. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  252. wpa_s->wpa_state == WPA_INACTIVE)
  253. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  254. /* Find the starting point from which to continue scanning */
  255. ssid = wpa_s->conf->ssid;
  256. if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
  257. while (ssid) {
  258. if (ssid == wpa_s->prev_scan_ssid) {
  259. ssid = ssid->next;
  260. break;
  261. }
  262. ssid = ssid->next;
  263. }
  264. }
  265. if (scan_req != 2 && (wpa_s->conf->ap_scan == 2 ||
  266. wpa_s->connect_without_scan)) {
  267. wpa_s->connect_without_scan = 0;
  268. wpa_supplicant_assoc_try(wpa_s, ssid);
  269. return;
  270. } else if (wpa_s->conf->ap_scan == 2) {
  271. /*
  272. * User-initiated scan request in ap_scan == 2; scan with
  273. * wildcard SSID.
  274. */
  275. ssid = NULL;
  276. } else {
  277. struct wpa_ssid *start = ssid, *tssid;
  278. int freqs_set = 0;
  279. if (ssid == NULL && max_ssids > 1)
  280. ssid = wpa_s->conf->ssid;
  281. while (ssid) {
  282. if (!ssid->disabled && ssid->scan_ssid) {
  283. wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
  284. ssid->ssid, ssid->ssid_len);
  285. params.ssids[params.num_ssids].ssid =
  286. ssid->ssid;
  287. params.ssids[params.num_ssids].ssid_len =
  288. ssid->ssid_len;
  289. params.num_ssids++;
  290. if (params.num_ssids + 1 >= max_ssids)
  291. break;
  292. }
  293. ssid = ssid->next;
  294. if (ssid == start)
  295. break;
  296. if (ssid == NULL && max_ssids > 1 &&
  297. start != wpa_s->conf->ssid)
  298. ssid = wpa_s->conf->ssid;
  299. }
  300. for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
  301. if (tssid->disabled)
  302. continue;
  303. if ((params.freqs || !freqs_set) && tssid->scan_freq) {
  304. int_array_concat(&params.freqs,
  305. tssid->scan_freq);
  306. } else {
  307. os_free(params.freqs);
  308. params.freqs = NULL;
  309. }
  310. freqs_set = 1;
  311. }
  312. int_array_sort_unique(params.freqs);
  313. }
  314. if (ssid) {
  315. wpa_s->prev_scan_ssid = ssid;
  316. if (max_ssids > 1) {
  317. wpa_printf(MSG_DEBUG, "Include wildcard SSID in the "
  318. "scan request");
  319. params.num_ssids++;
  320. }
  321. wpa_printf(MSG_DEBUG, "Starting AP scan for specific SSID(s)");
  322. } else {
  323. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  324. params.num_ssids++;
  325. wpa_printf(MSG_DEBUG, "Starting AP scan for wildcard SSID");
  326. }
  327. #ifdef CONFIG_P2P
  328. wpa_s->wps->dev.p2p = 1;
  329. if (!wps) {
  330. wps = 1;
  331. req_type = WPS_REQ_ENROLLEE_INFO;
  332. }
  333. if (params.freqs == NULL && wpa_s->p2p_in_provisioning &&
  334. wpa_s->go_params) {
  335. /* Optimize provisioning state scan based on GO information */
  336. if (wpa_s->p2p_in_provisioning < 5 &&
  337. wpa_s->go_params->freq > 0) {
  338. wpa_printf(MSG_DEBUG, "P2P: Scan only GO preferred "
  339. "frequency %d MHz",
  340. wpa_s->go_params->freq);
  341. params.freqs = os_zalloc(2 * sizeof(int));
  342. if (params.freqs)
  343. params.freqs[0] = wpa_s->go_params->freq;
  344. } else if (wpa_s->p2p_in_provisioning < 8 &&
  345. wpa_s->go_params->freq_list[0]) {
  346. wpa_printf(MSG_DEBUG, "P2P: Scan only common "
  347. "channels");
  348. int_array_concat(&params.freqs,
  349. wpa_s->go_params->freq_list);
  350. if (params.freqs)
  351. int_array_sort_unique(params.freqs);
  352. }
  353. wpa_s->p2p_in_provisioning++;
  354. }
  355. #endif /* CONFIG_P2P */
  356. #ifdef CONFIG_WPS
  357. if (params.freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
  358. /*
  359. * Optimize post-provisioning scan based on channel used
  360. * during provisioning.
  361. */
  362. wpa_printf(MSG_DEBUG, "WPS: Scan only frequency %u MHz that "
  363. "was used during provisioning", wpa_s->wps_freq);
  364. params.freqs = os_zalloc(2 * sizeof(int));
  365. if (params.freqs)
  366. params.freqs[0] = wpa_s->wps_freq;
  367. wpa_s->after_wps--;
  368. }
  369. if (wps) {
  370. wps_ie = wps_build_probe_req_ie(wps == 2, &wpa_s->wps->dev,
  371. wpa_s->wps->uuid, req_type);
  372. if (wps_ie) {
  373. params.extra_ies = wpabuf_head(wps_ie);
  374. params.extra_ies_len = wpabuf_len(wps_ie);
  375. }
  376. }
  377. #endif /* CONFIG_WPS */
  378. #ifdef CONFIG_P2P
  379. if (wps_ie) {
  380. if (wpabuf_resize(&wps_ie, 100) == 0) {
  381. wpas_p2p_scan_ie(wpa_s, wps_ie);
  382. params.extra_ies = wpabuf_head(wps_ie);
  383. params.extra_ies_len = wpabuf_len(wps_ie);
  384. }
  385. }
  386. #endif /* CONFIG_P2P */
  387. if (params.freqs == NULL && wpa_s->next_scan_freqs) {
  388. wpa_printf(MSG_DEBUG, "Optimize scan based on previously "
  389. "generated frequency list");
  390. params.freqs = wpa_s->next_scan_freqs;
  391. } else
  392. os_free(wpa_s->next_scan_freqs);
  393. wpa_s->next_scan_freqs = NULL;
  394. params.filter_ssids = wpa_supplicant_build_filter_ssids(
  395. wpa_s->conf, &params.num_filter_ssids);
  396. ret = wpa_supplicant_trigger_scan(wpa_s, &params);
  397. wpabuf_free(wps_ie);
  398. os_free(params.freqs);
  399. os_free(params.filter_ssids);
  400. if (ret) {
  401. wpa_printf(MSG_WARNING, "Failed to initiate AP scan.");
  402. if (prev_state != wpa_s->wpa_state)
  403. wpa_supplicant_set_state(wpa_s, prev_state);
  404. wpa_supplicant_req_scan(wpa_s, 1, 0);
  405. }
  406. }
  407. /**
  408. * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
  409. * @wpa_s: Pointer to wpa_supplicant data
  410. * @sec: Number of seconds after which to scan
  411. * @usec: Number of microseconds after which to scan
  412. *
  413. * This function is used to schedule a scan for neighboring access points after
  414. * the specified time.
  415. */
  416. void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
  417. {
  418. /* If there's at least one network that should be specifically scanned
  419. * then don't cancel the scan and reschedule. Some drivers do
  420. * background scanning which generates frequent scan results, and that
  421. * causes the specific SSID scan to get continually pushed back and
  422. * never happen, which causes hidden APs to never get probe-scanned.
  423. */
  424. if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
  425. wpa_s->conf->ap_scan == 1) {
  426. struct wpa_ssid *ssid = wpa_s->conf->ssid;
  427. while (ssid) {
  428. if (!ssid->disabled && ssid->scan_ssid)
  429. break;
  430. ssid = ssid->next;
  431. }
  432. if (ssid) {
  433. wpa_msg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
  434. "ensure that specific SSID scans occur");
  435. return;
  436. }
  437. }
  438. wpa_msg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
  439. sec, usec);
  440. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  441. eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
  442. }
  443. /**
  444. * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
  445. * @wpa_s: Pointer to wpa_supplicant data
  446. *
  447. * This function is used to cancel a scan request scheduled with
  448. * wpa_supplicant_req_scan().
  449. */
  450. void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
  451. {
  452. wpa_msg(wpa_s, MSG_DEBUG, "Cancelling scan request");
  453. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  454. }
  455. void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
  456. int scanning)
  457. {
  458. if (wpa_s->scanning != scanning) {
  459. wpa_s->scanning = scanning;
  460. wpas_notify_scanning(wpa_s);
  461. }
  462. }
  463. static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
  464. {
  465. int rate = 0;
  466. const u8 *ie;
  467. int i;
  468. ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
  469. for (i = 0; ie && i < ie[1]; i++) {
  470. if ((ie[i + 2] & 0x7f) > rate)
  471. rate = ie[i + 2] & 0x7f;
  472. }
  473. ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
  474. for (i = 0; ie && i < ie[1]; i++) {
  475. if ((ie[i + 2] & 0x7f) > rate)
  476. rate = ie[i + 2] & 0x7f;
  477. }
  478. return rate;
  479. }
  480. const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
  481. {
  482. const u8 *end, *pos;
  483. pos = (const u8 *) (res + 1);
  484. end = pos + res->ie_len;
  485. while (pos + 1 < end) {
  486. if (pos + 2 + pos[1] > end)
  487. break;
  488. if (pos[0] == ie)
  489. return pos;
  490. pos += 2 + pos[1];
  491. }
  492. return NULL;
  493. }
  494. const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
  495. u32 vendor_type)
  496. {
  497. const u8 *end, *pos;
  498. pos = (const u8 *) (res + 1);
  499. end = pos + res->ie_len;
  500. while (pos + 1 < end) {
  501. if (pos + 2 + pos[1] > end)
  502. break;
  503. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  504. vendor_type == WPA_GET_BE32(&pos[2]))
  505. return pos;
  506. pos += 2 + pos[1];
  507. }
  508. return NULL;
  509. }
  510. struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
  511. u32 vendor_type)
  512. {
  513. struct wpabuf *buf;
  514. const u8 *end, *pos;
  515. buf = wpabuf_alloc(res->ie_len);
  516. if (buf == NULL)
  517. return NULL;
  518. pos = (const u8 *) (res + 1);
  519. end = pos + res->ie_len;
  520. while (pos + 1 < end) {
  521. if (pos + 2 + pos[1] > end)
  522. break;
  523. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  524. vendor_type == WPA_GET_BE32(&pos[2]))
  525. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  526. pos += 2 + pos[1];
  527. }
  528. if (wpabuf_len(buf) == 0) {
  529. wpabuf_free(buf);
  530. buf = NULL;
  531. }
  532. return buf;
  533. }
  534. struct wpabuf * wpa_scan_get_vendor_ie_multi_beacon(
  535. const struct wpa_scan_res *res, u32 vendor_type)
  536. {
  537. struct wpabuf *buf;
  538. const u8 *end, *pos;
  539. if (res->beacon_ie_len == 0)
  540. return NULL;
  541. buf = wpabuf_alloc(res->beacon_ie_len);
  542. if (buf == NULL)
  543. return NULL;
  544. pos = (const u8 *) (res + 1);
  545. pos += res->ie_len;
  546. end = pos + res->beacon_ie_len;
  547. while (pos + 1 < end) {
  548. if (pos + 2 + pos[1] > end)
  549. break;
  550. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  551. vendor_type == WPA_GET_BE32(&pos[2]))
  552. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  553. pos += 2 + pos[1];
  554. }
  555. if (wpabuf_len(buf) == 0) {
  556. wpabuf_free(buf);
  557. buf = NULL;
  558. }
  559. return buf;
  560. }
  561. /* Compare function for sorting scan results. Return >0 if @b is considered
  562. * better. */
  563. static int wpa_scan_result_compar(const void *a, const void *b)
  564. {
  565. struct wpa_scan_res **_wa = (void *) a;
  566. struct wpa_scan_res **_wb = (void *) b;
  567. struct wpa_scan_res *wa = *_wa;
  568. struct wpa_scan_res *wb = *_wb;
  569. int wpa_a, wpa_b, maxrate_a, maxrate_b;
  570. /* WPA/WPA2 support preferred */
  571. wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
  572. wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
  573. wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
  574. wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
  575. if (wpa_b && !wpa_a)
  576. return 1;
  577. if (!wpa_b && wpa_a)
  578. return -1;
  579. /* privacy support preferred */
  580. if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
  581. (wb->caps & IEEE80211_CAP_PRIVACY))
  582. return 1;
  583. if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
  584. (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
  585. return -1;
  586. /* best/max rate preferred if signal level close enough XXX */
  587. if ((wa->level && wb->level && abs(wb->level - wa->level) < 5) ||
  588. (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
  589. maxrate_a = wpa_scan_get_max_rate(wa);
  590. maxrate_b = wpa_scan_get_max_rate(wb);
  591. if (maxrate_a != maxrate_b)
  592. return maxrate_b - maxrate_a;
  593. }
  594. /* use freq for channel preference */
  595. /* all things being equal, use signal level; if signal levels are
  596. * identical, use quality values since some drivers may only report
  597. * that value and leave the signal level zero */
  598. if (wb->level == wa->level)
  599. return wb->qual - wa->qual;
  600. return wb->level - wa->level;
  601. }
  602. #ifdef CONFIG_WPS
  603. /* Compare function for sorting scan results when searching a WPS AP for
  604. * provisioning. Return >0 if @b is considered better. */
  605. static int wpa_scan_result_wps_compar(const void *a, const void *b)
  606. {
  607. struct wpa_scan_res **_wa = (void *) a;
  608. struct wpa_scan_res **_wb = (void *) b;
  609. struct wpa_scan_res *wa = *_wa;
  610. struct wpa_scan_res *wb = *_wb;
  611. int uses_wps_a, uses_wps_b;
  612. struct wpabuf *wps_a, *wps_b;
  613. int res;
  614. /* Optimization - check WPS IE existence before allocated memory and
  615. * doing full reassembly. */
  616. uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
  617. uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
  618. if (uses_wps_a && !uses_wps_b)
  619. return -1;
  620. if (!uses_wps_a && uses_wps_b)
  621. return 1;
  622. if (uses_wps_a && uses_wps_b) {
  623. wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
  624. wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
  625. res = wps_ap_priority_compar(wps_a, wps_b);
  626. wpabuf_free(wps_a);
  627. wpabuf_free(wps_b);
  628. if (res)
  629. return res;
  630. }
  631. /*
  632. * Do not use current AP security policy as a sorting criteria during
  633. * WPS provisioning step since the AP may get reconfigured at the
  634. * completion of provisioning.
  635. */
  636. /* all things being equal, use signal level; if signal levels are
  637. * identical, use quality values since some drivers may only report
  638. * that value and leave the signal level zero */
  639. if (wb->level == wa->level)
  640. return wb->qual - wa->qual;
  641. return wb->level - wa->level;
  642. }
  643. #endif /* CONFIG_WPS */
  644. /**
  645. * wpa_supplicant_get_scan_results - Get scan results
  646. * @wpa_s: Pointer to wpa_supplicant data
  647. * @info: Information about what was scanned or %NULL if not available
  648. * @new_scan: Whether a new scan was performed
  649. * Returns: Scan results, %NULL on failure
  650. *
  651. * This function request the current scan results from the driver and updates
  652. * the local BSS list wpa_s->bss. The caller is responsible for freeing the
  653. * results with wpa_scan_results_free().
  654. */
  655. struct wpa_scan_results *
  656. wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
  657. struct scan_info *info, int new_scan)
  658. {
  659. struct wpa_scan_results *scan_res;
  660. size_t i;
  661. int (*compar)(const void *, const void *) = wpa_scan_result_compar;
  662. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  663. scan_res = ieee80211_sta_get_scan_results(wpa_s);
  664. else
  665. scan_res = wpa_drv_get_scan_results2(wpa_s);
  666. if (scan_res == NULL) {
  667. wpa_printf(MSG_DEBUG, "Failed to get scan results");
  668. return NULL;
  669. }
  670. #ifdef CONFIG_WPS
  671. if (wpas_wps_in_progress(wpa_s)) {
  672. wpa_printf(MSG_DEBUG, "WPS: Order scan results with WPS "
  673. "provisioning rules");
  674. compar = wpa_scan_result_wps_compar;
  675. }
  676. #endif /* CONFIG_WPS */
  677. qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
  678. compar);
  679. wpa_bss_update_start(wpa_s);
  680. for (i = 0; i < scan_res->num; i++)
  681. wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
  682. wpa_bss_update_end(wpa_s, info, new_scan);
  683. return scan_res;
  684. }
  685. int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
  686. {
  687. struct wpa_scan_results *scan_res;
  688. scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
  689. if (scan_res == NULL)
  690. return -1;
  691. wpa_scan_results_free(scan_res);
  692. return 0;
  693. }
  694. void wpa_scan_results_free(struct wpa_scan_results *res)
  695. {
  696. size_t i;
  697. if (res == NULL)
  698. return;
  699. for (i = 0; i < res->num; i++)
  700. os_free(res->res[i]);
  701. os_free(res->res);
  702. os_free(res);
  703. }