Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

scan.c 20KB

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