scan.c 20 KB

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