scan.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  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 "wps_supplicant.h"
  22. #include "p2p_supplicant.h"
  23. #include "p2p/p2p.h"
  24. #include "notify.h"
  25. #include "bss.h"
  26. #include "scan.h"
  27. static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
  28. {
  29. struct wpa_ssid *ssid;
  30. union wpa_event_data data;
  31. ssid = wpa_supplicant_get_ssid(wpa_s);
  32. if (ssid == NULL)
  33. return;
  34. if (wpa_s->current_ssid == NULL) {
  35. wpa_s->current_ssid = ssid;
  36. if (wpa_s->current_ssid != NULL)
  37. wpas_notify_network_changed(wpa_s);
  38. }
  39. wpa_supplicant_initiate_eapol(wpa_s);
  40. wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured "
  41. "network - generating associated event");
  42. os_memset(&data, 0, sizeof(data));
  43. wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
  44. }
  45. #ifdef CONFIG_WPS
  46. static int wpas_wps_in_use(struct wpa_supplicant *wpa_s,
  47. enum wps_request_type *req_type)
  48. {
  49. struct wpa_ssid *ssid;
  50. int wps = 0;
  51. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  52. if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
  53. continue;
  54. wps = 1;
  55. *req_type = wpas_wps_get_req_type(ssid);
  56. if (!ssid->eap.phase1)
  57. continue;
  58. if (os_strstr(ssid->eap.phase1, "pbc=1"))
  59. return 2;
  60. }
  61. #ifdef CONFIG_P2P
  62. wpa_s->wps->dev.p2p = 1;
  63. if (!wps) {
  64. wps = 1;
  65. *req_type = WPS_REQ_ENROLLEE_INFO;
  66. }
  67. #endif /* CONFIG_P2P */
  68. return wps;
  69. }
  70. #endif /* CONFIG_WPS */
  71. int wpa_supplicant_enabled_networks(struct wpa_config *conf)
  72. {
  73. struct wpa_ssid *ssid = conf->ssid;
  74. int count = 0;
  75. while (ssid) {
  76. if (!ssid->disabled)
  77. count++;
  78. ssid = ssid->next;
  79. }
  80. return count;
  81. }
  82. static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
  83. struct wpa_ssid *ssid)
  84. {
  85. while (ssid) {
  86. if (!ssid->disabled)
  87. break;
  88. ssid = ssid->next;
  89. }
  90. /* ap_scan=2 mode - try to associate with each SSID. */
  91. if (ssid == NULL) {
  92. wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
  93. "end of scan list - go back to beginning");
  94. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  95. wpa_supplicant_req_scan(wpa_s, 0, 0);
  96. return;
  97. }
  98. if (ssid->next) {
  99. /* Continue from the next SSID on the next attempt. */
  100. wpa_s->prev_scan_ssid = ssid;
  101. } else {
  102. /* Start from the beginning of the SSID list. */
  103. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  104. }
  105. wpa_supplicant_associate(wpa_s, NULL, ssid);
  106. }
  107. static int int_array_len(const int *a)
  108. {
  109. int i;
  110. for (i = 0; a && a[i]; i++)
  111. ;
  112. return i;
  113. }
  114. static void int_array_concat(int **res, const int *a)
  115. {
  116. int reslen, alen, i;
  117. int *n;
  118. reslen = int_array_len(*res);
  119. alen = int_array_len(a);
  120. n = os_realloc(*res, (reslen + alen + 1) * sizeof(int));
  121. if (n == NULL) {
  122. os_free(*res);
  123. *res = NULL;
  124. return;
  125. }
  126. for (i = 0; i <= alen; i++)
  127. n[reslen + i] = a[i];
  128. *res = n;
  129. }
  130. static int freq_cmp(const void *a, const void *b)
  131. {
  132. int _a = *(int *) a;
  133. int _b = *(int *) b;
  134. if (_a == 0)
  135. return 1;
  136. if (_b == 0)
  137. return -1;
  138. return _a - _b;
  139. }
  140. static void int_array_sort_unique(int *a)
  141. {
  142. int alen;
  143. int i, j;
  144. if (a == NULL)
  145. return;
  146. alen = int_array_len(a);
  147. qsort(a, alen, sizeof(int), freq_cmp);
  148. i = 0;
  149. j = 1;
  150. while (a[i] && a[j]) {
  151. if (a[i] == a[j]) {
  152. j++;
  153. continue;
  154. }
  155. a[++i] = a[j++];
  156. }
  157. if (a[i])
  158. i++;
  159. a[i] = 0;
  160. }
  161. int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
  162. struct wpa_driver_scan_params *params)
  163. {
  164. int ret;
  165. wpa_supplicant_notify_scanning(wpa_s, 1);
  166. ret = wpa_drv_scan(wpa_s, params);
  167. if (ret) {
  168. wpa_supplicant_notify_scanning(wpa_s, 0);
  169. wpas_notify_scan_done(wpa_s, 0);
  170. } else {
  171. wpa_s->scan_runs++;
  172. wpa_s->normal_scans++;
  173. }
  174. return ret;
  175. }
  176. static void
  177. wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  178. {
  179. struct wpa_supplicant *wpa_s = eloop_ctx;
  180. wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
  181. if (wpa_supplicant_req_sched_scan(wpa_s))
  182. wpa_supplicant_req_scan(wpa_s, 0, 0);
  183. }
  184. static void
  185. wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  186. {
  187. struct wpa_supplicant *wpa_s = eloop_ctx;
  188. wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
  189. wpa_s->sched_scan_timed_out = 1;
  190. wpa_supplicant_cancel_sched_scan(wpa_s);
  191. }
  192. static int
  193. wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
  194. struct wpa_driver_scan_params *params,
  195. int interval)
  196. {
  197. int ret;
  198. wpa_supplicant_notify_scanning(wpa_s, 1);
  199. ret = wpa_drv_sched_scan(wpa_s, params, interval * 1000);
  200. if (ret)
  201. wpa_supplicant_notify_scanning(wpa_s, 0);
  202. else
  203. wpa_s->sched_scanning = 1;
  204. return ret;
  205. }
  206. static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
  207. {
  208. int ret;
  209. ret = wpa_drv_stop_sched_scan(wpa_s);
  210. if (ret) {
  211. wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
  212. /* TODO: what to do if stopping fails? */
  213. return -1;
  214. }
  215. return ret;
  216. }
  217. static struct wpa_driver_scan_filter *
  218. wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
  219. {
  220. struct wpa_driver_scan_filter *ssids;
  221. struct wpa_ssid *ssid;
  222. size_t count;
  223. *num_ssids = 0;
  224. if (!conf->filter_ssids)
  225. return NULL;
  226. for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
  227. if (ssid->ssid && ssid->ssid_len)
  228. count++;
  229. }
  230. if (count == 0)
  231. return NULL;
  232. ssids = os_zalloc(count * sizeof(struct wpa_driver_scan_filter));
  233. if (ssids == NULL)
  234. return NULL;
  235. for (ssid = conf->ssid; ssid; ssid = ssid->next) {
  236. if (!ssid->ssid || !ssid->ssid_len)
  237. continue;
  238. os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
  239. ssids[*num_ssids].ssid_len = ssid->ssid_len;
  240. (*num_ssids)++;
  241. }
  242. return ssids;
  243. }
  244. static void wpa_supplicant_optimize_freqs(
  245. struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
  246. {
  247. #ifdef CONFIG_P2P
  248. if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
  249. wpa_s->go_params) {
  250. /* Optimize provisioning state scan based on GO information */
  251. if (wpa_s->p2p_in_provisioning < 5 &&
  252. wpa_s->go_params->freq > 0) {
  253. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
  254. "preferred frequency %d MHz",
  255. wpa_s->go_params->freq);
  256. params->freqs = os_zalloc(2 * sizeof(int));
  257. if (params->freqs)
  258. params->freqs[0] = wpa_s->go_params->freq;
  259. } else if (wpa_s->p2p_in_provisioning < 8 &&
  260. wpa_s->go_params->freq_list[0]) {
  261. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
  262. "channels");
  263. int_array_concat(&params->freqs,
  264. wpa_s->go_params->freq_list);
  265. if (params->freqs)
  266. int_array_sort_unique(params->freqs);
  267. }
  268. wpa_s->p2p_in_provisioning++;
  269. }
  270. #endif /* CONFIG_P2P */
  271. #ifdef CONFIG_WPS
  272. if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
  273. /*
  274. * Optimize post-provisioning scan based on channel used
  275. * during provisioning.
  276. */
  277. wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
  278. "that was used during provisioning", wpa_s->wps_freq);
  279. params->freqs = os_zalloc(2 * sizeof(int));
  280. if (params->freqs)
  281. params->freqs[0] = wpa_s->wps_freq;
  282. wpa_s->after_wps--;
  283. }
  284. #endif /* CONFIG_WPS */
  285. }
  286. #ifdef CONFIG_INTERWORKING
  287. static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
  288. struct wpabuf *buf)
  289. {
  290. if (wpa_s->conf->interworking == 0)
  291. return;
  292. wpabuf_put_u8(buf, WLAN_EID_EXT_CAPAB);
  293. wpabuf_put_u8(buf, 4);
  294. wpabuf_put_u8(buf, 0x00);
  295. wpabuf_put_u8(buf, 0x00);
  296. wpabuf_put_u8(buf, 0x00);
  297. wpabuf_put_u8(buf, 0x80); /* Bit 31 - Interworking */
  298. wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
  299. wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
  300. 1 + ETH_ALEN);
  301. wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
  302. /* No Venue Info */
  303. if (!is_zero_ether_addr(wpa_s->conf->hessid))
  304. wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
  305. }
  306. #endif /* CONFIG_INTERWORKING */
  307. static struct wpabuf *
  308. wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s,
  309. struct wpa_driver_scan_params *params)
  310. {
  311. struct wpabuf *extra_ie = NULL;
  312. #ifdef CONFIG_WPS
  313. int wps = 0;
  314. enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
  315. #endif /* CONFIG_WPS */
  316. #ifdef CONFIG_INTERWORKING
  317. if (wpa_s->conf->interworking &&
  318. wpabuf_resize(&extra_ie, 100) == 0)
  319. wpas_add_interworking_elements(wpa_s, extra_ie);
  320. #endif /* CONFIG_INTERWORKING */
  321. #ifdef CONFIG_WPS
  322. wps = wpas_wps_in_use(wpa_s, &req_type);
  323. if (wps) {
  324. struct wpabuf *wps_ie;
  325. wps_ie = wps_build_probe_req_ie(wps == 2, &wpa_s->wps->dev,
  326. wpa_s->wps->uuid, req_type,
  327. 0, NULL);
  328. if (wps_ie) {
  329. if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
  330. wpabuf_put_buf(extra_ie, wps_ie);
  331. wpabuf_free(wps_ie);
  332. }
  333. }
  334. #ifdef CONFIG_P2P
  335. if (wps) {
  336. size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  337. if (wpabuf_resize(&extra_ie, ielen) == 0)
  338. wpas_p2p_scan_ie(wpa_s, extra_ie);
  339. }
  340. #endif /* CONFIG_P2P */
  341. #endif /* CONFIG_WPS */
  342. return extra_ie;
  343. }
  344. static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
  345. {
  346. struct wpa_supplicant *wpa_s = eloop_ctx;
  347. struct wpa_ssid *ssid;
  348. int scan_req = 0, ret;
  349. struct wpabuf *extra_ie;
  350. struct wpa_driver_scan_params params;
  351. size_t max_ssids;
  352. enum wpa_states prev_state;
  353. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  354. wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
  355. return;
  356. }
  357. if (wpa_s->disconnected && !wpa_s->scan_req) {
  358. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  359. return;
  360. }
  361. if (!wpa_supplicant_enabled_networks(wpa_s->conf) &&
  362. !wpa_s->scan_req) {
  363. wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
  364. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  365. return;
  366. }
  367. if (wpa_s->conf->ap_scan != 0 &&
  368. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
  369. wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
  370. "overriding ap_scan configuration");
  371. wpa_s->conf->ap_scan = 0;
  372. wpas_notify_ap_scan_changed(wpa_s);
  373. }
  374. if (wpa_s->conf->ap_scan == 0) {
  375. wpa_supplicant_gen_assoc_event(wpa_s);
  376. return;
  377. }
  378. #ifdef CONFIG_P2P
  379. if (wpas_p2p_in_progress(wpa_s)) {
  380. if (wpa_s->wpa_state == WPA_SCANNING) {
  381. wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan "
  382. "while P2P operation is in progress");
  383. wpa_supplicant_req_scan(wpa_s, 5, 0);
  384. } else {
  385. wpa_dbg(wpa_s, MSG_DEBUG, "Do not request scan while "
  386. "P2P operation is in progress");
  387. }
  388. return;
  389. }
  390. #endif /* CONFIG_P2P */
  391. if (wpa_s->conf->ap_scan == 2)
  392. max_ssids = 1;
  393. else {
  394. max_ssids = wpa_s->max_scan_ssids;
  395. if (max_ssids > WPAS_MAX_SCAN_SSIDS)
  396. max_ssids = WPAS_MAX_SCAN_SSIDS;
  397. }
  398. scan_req = wpa_s->scan_req;
  399. wpa_s->scan_req = 0;
  400. os_memset(&params, 0, sizeof(params));
  401. prev_state = wpa_s->wpa_state;
  402. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  403. wpa_s->wpa_state == WPA_INACTIVE)
  404. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  405. if (scan_req != 2 && wpa_s->connect_without_scan) {
  406. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  407. if (ssid == wpa_s->connect_without_scan)
  408. break;
  409. }
  410. wpa_s->connect_without_scan = NULL;
  411. if (ssid) {
  412. wpa_printf(MSG_DEBUG, "Start a pre-selected network "
  413. "without scan step");
  414. wpa_supplicant_associate(wpa_s, NULL, ssid);
  415. return;
  416. }
  417. }
  418. /* Find the starting point from which to continue scanning */
  419. ssid = wpa_s->conf->ssid;
  420. if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
  421. while (ssid) {
  422. if (ssid == wpa_s->prev_scan_ssid) {
  423. ssid = ssid->next;
  424. break;
  425. }
  426. ssid = ssid->next;
  427. }
  428. }
  429. if (scan_req != 2 && wpa_s->conf->ap_scan == 2) {
  430. wpa_s->connect_without_scan = NULL;
  431. wpa_supplicant_assoc_try(wpa_s, ssid);
  432. return;
  433. } else if (wpa_s->conf->ap_scan == 2) {
  434. /*
  435. * User-initiated scan request in ap_scan == 2; scan with
  436. * wildcard SSID.
  437. */
  438. ssid = NULL;
  439. } else {
  440. struct wpa_ssid *start = ssid, *tssid;
  441. int freqs_set = 0;
  442. if (ssid == NULL && max_ssids > 1)
  443. ssid = wpa_s->conf->ssid;
  444. while (ssid) {
  445. if (!ssid->disabled && ssid->scan_ssid) {
  446. wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
  447. ssid->ssid, ssid->ssid_len);
  448. params.ssids[params.num_ssids].ssid =
  449. ssid->ssid;
  450. params.ssids[params.num_ssids].ssid_len =
  451. ssid->ssid_len;
  452. params.num_ssids++;
  453. if (params.num_ssids + 1 >= max_ssids)
  454. break;
  455. }
  456. ssid = ssid->next;
  457. if (ssid == start)
  458. break;
  459. if (ssid == NULL && max_ssids > 1 &&
  460. start != wpa_s->conf->ssid)
  461. ssid = wpa_s->conf->ssid;
  462. }
  463. for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
  464. if (tssid->disabled)
  465. continue;
  466. if ((params.freqs || !freqs_set) && tssid->scan_freq) {
  467. int_array_concat(&params.freqs,
  468. tssid->scan_freq);
  469. } else {
  470. os_free(params.freqs);
  471. params.freqs = NULL;
  472. }
  473. freqs_set = 1;
  474. }
  475. int_array_sort_unique(params.freqs);
  476. }
  477. if (ssid) {
  478. wpa_s->prev_scan_ssid = ssid;
  479. if (max_ssids > 1) {
  480. wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
  481. "the scan request");
  482. params.num_ssids++;
  483. }
  484. wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for specific "
  485. "SSID(s)");
  486. } else {
  487. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  488. params.num_ssids++;
  489. wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
  490. "SSID");
  491. }
  492. wpa_supplicant_optimize_freqs(wpa_s, &params);
  493. extra_ie = wpa_supplicant_extra_ies(wpa_s, &params);
  494. if (params.freqs == NULL && wpa_s->next_scan_freqs) {
  495. wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
  496. "generated frequency list");
  497. params.freqs = wpa_s->next_scan_freqs;
  498. } else
  499. os_free(wpa_s->next_scan_freqs);
  500. wpa_s->next_scan_freqs = NULL;
  501. params.filter_ssids = wpa_supplicant_build_filter_ssids(
  502. wpa_s->conf, &params.num_filter_ssids);
  503. if (extra_ie) {
  504. params.extra_ies = wpabuf_head(extra_ie);
  505. params.extra_ies_len = wpabuf_len(extra_ie);
  506. }
  507. #ifdef CONFIG_P2P
  508. if (wpa_s->p2p_in_provisioning) {
  509. /*
  510. * The interface may not yet be in P2P mode, so we have to
  511. * explicitly request P2P probe to disable CCK rates.
  512. */
  513. params.p2p_probe = 1;
  514. }
  515. #endif /* CONFIG_P2P */
  516. ret = wpa_supplicant_trigger_scan(wpa_s, &params);
  517. wpabuf_free(extra_ie);
  518. os_free(params.freqs);
  519. os_free(params.filter_ssids);
  520. if (ret) {
  521. wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
  522. if (prev_state != wpa_s->wpa_state)
  523. wpa_supplicant_set_state(wpa_s, prev_state);
  524. wpa_supplicant_req_scan(wpa_s, 1, 0);
  525. }
  526. }
  527. /**
  528. * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
  529. * @wpa_s: Pointer to wpa_supplicant data
  530. * @sec: Number of seconds after which to scan
  531. * @usec: Number of microseconds after which to scan
  532. *
  533. * This function is used to schedule a scan for neighboring access points after
  534. * the specified time.
  535. */
  536. void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
  537. {
  538. /* If there's at least one network that should be specifically scanned
  539. * then don't cancel the scan and reschedule. Some drivers do
  540. * background scanning which generates frequent scan results, and that
  541. * causes the specific SSID scan to get continually pushed back and
  542. * never happen, which causes hidden APs to never get probe-scanned.
  543. */
  544. if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
  545. wpa_s->conf->ap_scan == 1) {
  546. struct wpa_ssid *ssid = wpa_s->conf->ssid;
  547. while (ssid) {
  548. if (!ssid->disabled && ssid->scan_ssid)
  549. break;
  550. ssid = ssid->next;
  551. }
  552. if (ssid) {
  553. wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
  554. "ensure that specific SSID scans occur");
  555. return;
  556. }
  557. }
  558. wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
  559. sec, usec);
  560. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  561. eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
  562. }
  563. /**
  564. * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
  565. * @wpa_s: Pointer to wpa_supplicant data
  566. * @sec: Number of seconds after which to scan
  567. * @usec: Number of microseconds after which to scan
  568. *
  569. * This function is used to schedule periodic scans for neighboring
  570. * access points after the specified time.
  571. */
  572. int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
  573. int sec, int usec)
  574. {
  575. if (!wpa_s->sched_scan_supported)
  576. return -1;
  577. eloop_register_timeout(sec, usec,
  578. wpa_supplicant_delayed_sched_scan_timeout,
  579. wpa_s, NULL);
  580. return 0;
  581. }
  582. /**
  583. * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
  584. * @wpa_s: Pointer to wpa_supplicant data
  585. *
  586. * This function is used to schedule periodic scans for neighboring
  587. * access points repeating the scan continuously.
  588. */
  589. int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
  590. {
  591. struct wpa_driver_scan_params params;
  592. enum wpa_states prev_state;
  593. struct wpa_ssid *ssid;
  594. struct wpabuf *wps_ie = NULL;
  595. int ret;
  596. unsigned int max_sched_scan_ssids;
  597. int wildcard = 0;
  598. int need_ssids;
  599. if (!wpa_s->sched_scan_supported)
  600. return -1;
  601. if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
  602. max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
  603. else
  604. max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
  605. if (max_sched_scan_ssids < 1)
  606. return -1;
  607. if (wpa_s->sched_scanning) {
  608. wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
  609. return 0;
  610. }
  611. need_ssids = 0;
  612. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  613. if (!ssid->disabled && !ssid->scan_ssid) {
  614. /* Use wildcard SSID to find this network */
  615. wildcard = 1;
  616. } else if (!ssid->disabled && ssid->ssid_len)
  617. need_ssids++;
  618. }
  619. if (wildcard)
  620. need_ssids++;
  621. if (wpa_s->normal_scans < 3 &&
  622. (need_ssids <= wpa_s->max_scan_ssids ||
  623. wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
  624. /*
  625. * When normal scan can speed up operations, use that for the
  626. * first operations before starting the sched_scan to allow
  627. * user space sleep more. We do this only if the normal scan
  628. * has functionality that is suitable for this or if the
  629. * sched_scan does not have better support for multiple SSIDs.
  630. */
  631. wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
  632. "sched_scan for initial scans (normal_scans=%d)",
  633. wpa_s->normal_scans);
  634. return -1;
  635. }
  636. os_memset(&params, 0, sizeof(params));
  637. /* If we can't allocate space for the filters, we just don't filter */
  638. params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
  639. sizeof(struct wpa_driver_scan_filter));
  640. prev_state = wpa_s->wpa_state;
  641. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  642. wpa_s->wpa_state == WPA_INACTIVE)
  643. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  644. /* Find the starting point from which to continue scanning */
  645. ssid = wpa_s->conf->ssid;
  646. if (wpa_s->prev_sched_ssid) {
  647. while (ssid) {
  648. if (ssid == wpa_s->prev_sched_ssid) {
  649. ssid = ssid->next;
  650. break;
  651. }
  652. ssid = ssid->next;
  653. }
  654. }
  655. if (!ssid || !wpa_s->prev_sched_ssid) {
  656. wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
  657. wpa_s->sched_scan_interval = 10;
  658. wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
  659. wpa_s->first_sched_scan = 1;
  660. ssid = wpa_s->conf->ssid;
  661. wpa_s->prev_sched_ssid = ssid;
  662. }
  663. if (wildcard) {
  664. wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
  665. params.num_ssids++;
  666. }
  667. while (ssid) {
  668. if (ssid->disabled)
  669. goto next;
  670. if (params.num_filter_ssids < wpa_s->max_match_sets &&
  671. params.filter_ssids && ssid->ssid && ssid->ssid_len) {
  672. wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
  673. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  674. os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
  675. ssid->ssid, ssid->ssid_len);
  676. params.filter_ssids[params.num_filter_ssids].ssid_len =
  677. ssid->ssid_len;
  678. params.num_filter_ssids++;
  679. } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
  680. {
  681. wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
  682. "filter for sched_scan - drop filter");
  683. os_free(params.filter_ssids);
  684. params.filter_ssids = NULL;
  685. params.num_filter_ssids = 0;
  686. }
  687. if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
  688. if (params.num_ssids == max_sched_scan_ssids)
  689. break; /* only room for broadcast SSID */
  690. wpa_dbg(wpa_s, MSG_DEBUG,
  691. "add to active scan ssid: %s",
  692. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  693. params.ssids[params.num_ssids].ssid =
  694. ssid->ssid;
  695. params.ssids[params.num_ssids].ssid_len =
  696. ssid->ssid_len;
  697. params.num_ssids++;
  698. if (params.num_ssids >= max_sched_scan_ssids) {
  699. wpa_s->prev_sched_ssid = ssid;
  700. break;
  701. }
  702. }
  703. next:
  704. wpa_s->prev_sched_ssid = ssid;
  705. ssid = ssid->next;
  706. }
  707. if (params.num_filter_ssids == 0) {
  708. os_free(params.filter_ssids);
  709. params.filter_ssids = NULL;
  710. }
  711. if (wpa_s->wps)
  712. wps_ie = wpa_supplicant_extra_ies(wpa_s, &params);
  713. if (ssid || !wpa_s->first_sched_scan) {
  714. wpa_dbg(wpa_s, MSG_DEBUG,
  715. "Starting sched scan: interval %d (no timeout)",
  716. wpa_s->sched_scan_interval);
  717. } else {
  718. wpa_dbg(wpa_s, MSG_DEBUG,
  719. "Starting sched scan: interval %d timeout %d",
  720. wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
  721. }
  722. ret = wpa_supplicant_start_sched_scan(wpa_s, &params,
  723. wpa_s->sched_scan_interval);
  724. wpabuf_free(wps_ie);
  725. os_free(params.filter_ssids);
  726. if (ret) {
  727. wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
  728. if (prev_state != wpa_s->wpa_state)
  729. wpa_supplicant_set_state(wpa_s, prev_state);
  730. return ret;
  731. }
  732. /* If we have more SSIDs to scan, add a timeout so we scan them too */
  733. if (ssid || !wpa_s->first_sched_scan) {
  734. wpa_s->sched_scan_timed_out = 0;
  735. eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
  736. wpa_supplicant_sched_scan_timeout,
  737. wpa_s, NULL);
  738. wpa_s->first_sched_scan = 0;
  739. wpa_s->sched_scan_timeout /= 2;
  740. wpa_s->sched_scan_interval *= 2;
  741. }
  742. return 0;
  743. }
  744. /**
  745. * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
  746. * @wpa_s: Pointer to wpa_supplicant data
  747. *
  748. * This function is used to cancel a scan request scheduled with
  749. * wpa_supplicant_req_scan().
  750. */
  751. void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
  752. {
  753. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
  754. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  755. }
  756. /**
  757. * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
  758. * @wpa_s: Pointer to wpa_supplicant data
  759. *
  760. * This function is used to stop a periodic scheduled scan.
  761. */
  762. void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
  763. {
  764. if (!wpa_s->sched_scanning)
  765. return;
  766. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
  767. eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
  768. wpa_supplicant_stop_sched_scan(wpa_s);
  769. }
  770. void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
  771. int scanning)
  772. {
  773. if (wpa_s->scanning != scanning) {
  774. wpa_s->scanning = scanning;
  775. wpas_notify_scanning(wpa_s);
  776. }
  777. }
  778. static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
  779. {
  780. int rate = 0;
  781. const u8 *ie;
  782. int i;
  783. ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
  784. for (i = 0; ie && i < ie[1]; i++) {
  785. if ((ie[i + 2] & 0x7f) > rate)
  786. rate = ie[i + 2] & 0x7f;
  787. }
  788. ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
  789. for (i = 0; ie && i < ie[1]; i++) {
  790. if ((ie[i + 2] & 0x7f) > rate)
  791. rate = ie[i + 2] & 0x7f;
  792. }
  793. return rate;
  794. }
  795. const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
  796. {
  797. const u8 *end, *pos;
  798. pos = (const u8 *) (res + 1);
  799. end = pos + res->ie_len;
  800. while (pos + 1 < end) {
  801. if (pos + 2 + pos[1] > end)
  802. break;
  803. if (pos[0] == ie)
  804. return pos;
  805. pos += 2 + pos[1];
  806. }
  807. return NULL;
  808. }
  809. const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
  810. u32 vendor_type)
  811. {
  812. const u8 *end, *pos;
  813. pos = (const u8 *) (res + 1);
  814. end = pos + res->ie_len;
  815. while (pos + 1 < end) {
  816. if (pos + 2 + pos[1] > end)
  817. break;
  818. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  819. vendor_type == WPA_GET_BE32(&pos[2]))
  820. return pos;
  821. pos += 2 + pos[1];
  822. }
  823. return NULL;
  824. }
  825. struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
  826. u32 vendor_type)
  827. {
  828. struct wpabuf *buf;
  829. const u8 *end, *pos;
  830. buf = wpabuf_alloc(res->ie_len);
  831. if (buf == NULL)
  832. return NULL;
  833. pos = (const u8 *) (res + 1);
  834. end = pos + res->ie_len;
  835. while (pos + 1 < end) {
  836. if (pos + 2 + pos[1] > end)
  837. break;
  838. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  839. vendor_type == WPA_GET_BE32(&pos[2]))
  840. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  841. pos += 2 + pos[1];
  842. }
  843. if (wpabuf_len(buf) == 0) {
  844. wpabuf_free(buf);
  845. buf = NULL;
  846. }
  847. return buf;
  848. }
  849. struct wpabuf * wpa_scan_get_vendor_ie_multi_beacon(
  850. const struct wpa_scan_res *res, u32 vendor_type)
  851. {
  852. struct wpabuf *buf;
  853. const u8 *end, *pos;
  854. if (res->beacon_ie_len == 0)
  855. return NULL;
  856. buf = wpabuf_alloc(res->beacon_ie_len);
  857. if (buf == NULL)
  858. return NULL;
  859. pos = (const u8 *) (res + 1);
  860. pos += res->ie_len;
  861. end = pos + res->beacon_ie_len;
  862. while (pos + 1 < end) {
  863. if (pos + 2 + pos[1] > end)
  864. break;
  865. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  866. vendor_type == WPA_GET_BE32(&pos[2]))
  867. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  868. pos += 2 + pos[1];
  869. }
  870. if (wpabuf_len(buf) == 0) {
  871. wpabuf_free(buf);
  872. buf = NULL;
  873. }
  874. return buf;
  875. }
  876. /*
  877. * Channels with a great SNR can operate at full rate. What is a great SNR?
  878. * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
  879. * rule of thumb is that any SNR above 20 is good." This one
  880. * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
  881. * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
  882. * conservative value.
  883. */
  884. #define GREAT_SNR 30
  885. /* Compare function for sorting scan results. Return >0 if @b is considered
  886. * better. */
  887. static int wpa_scan_result_compar(const void *a, const void *b)
  888. {
  889. #define IS_5GHZ(n) (n > 4000)
  890. #define MIN(a,b) a < b ? a : b
  891. struct wpa_scan_res **_wa = (void *) a;
  892. struct wpa_scan_res **_wb = (void *) b;
  893. struct wpa_scan_res *wa = *_wa;
  894. struct wpa_scan_res *wb = *_wb;
  895. int wpa_a, wpa_b, maxrate_a, maxrate_b;
  896. int snr_a, snr_b;
  897. /* WPA/WPA2 support preferred */
  898. wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
  899. wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
  900. wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
  901. wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
  902. if (wpa_b && !wpa_a)
  903. return 1;
  904. if (!wpa_b && wpa_a)
  905. return -1;
  906. /* privacy support preferred */
  907. if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
  908. (wb->caps & IEEE80211_CAP_PRIVACY))
  909. return 1;
  910. if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
  911. (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
  912. return -1;
  913. if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
  914. !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
  915. snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
  916. snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
  917. } else {
  918. /* Not suitable information to calculate SNR, so use level */
  919. snr_a = wa->level;
  920. snr_b = wb->level;
  921. }
  922. /* best/max rate preferred if SNR close enough */
  923. if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
  924. (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
  925. maxrate_a = wpa_scan_get_max_rate(wa);
  926. maxrate_b = wpa_scan_get_max_rate(wb);
  927. if (maxrate_a != maxrate_b)
  928. return maxrate_b - maxrate_a;
  929. if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
  930. return IS_5GHZ(wa->freq) ? -1 : 1;
  931. }
  932. /* use freq for channel preference */
  933. /* all things being equal, use SNR; if SNRs are
  934. * identical, use quality values since some drivers may only report
  935. * that value and leave the signal level zero */
  936. if (snr_b == snr_a)
  937. return wb->qual - wa->qual;
  938. return snr_b - snr_a;
  939. #undef MIN
  940. #undef IS_5GHZ
  941. }
  942. #ifdef CONFIG_WPS
  943. /* Compare function for sorting scan results when searching a WPS AP for
  944. * provisioning. Return >0 if @b is considered better. */
  945. static int wpa_scan_result_wps_compar(const void *a, const void *b)
  946. {
  947. struct wpa_scan_res **_wa = (void *) a;
  948. struct wpa_scan_res **_wb = (void *) b;
  949. struct wpa_scan_res *wa = *_wa;
  950. struct wpa_scan_res *wb = *_wb;
  951. int uses_wps_a, uses_wps_b;
  952. struct wpabuf *wps_a, *wps_b;
  953. int res;
  954. /* Optimization - check WPS IE existence before allocated memory and
  955. * doing full reassembly. */
  956. uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
  957. uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
  958. if (uses_wps_a && !uses_wps_b)
  959. return -1;
  960. if (!uses_wps_a && uses_wps_b)
  961. return 1;
  962. if (uses_wps_a && uses_wps_b) {
  963. wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
  964. wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
  965. res = wps_ap_priority_compar(wps_a, wps_b);
  966. wpabuf_free(wps_a);
  967. wpabuf_free(wps_b);
  968. if (res)
  969. return res;
  970. }
  971. /*
  972. * Do not use current AP security policy as a sorting criteria during
  973. * WPS provisioning step since the AP may get reconfigured at the
  974. * completion of provisioning.
  975. */
  976. /* all things being equal, use signal level; if signal levels are
  977. * identical, use quality values since some drivers may only report
  978. * that value and leave the signal level zero */
  979. if (wb->level == wa->level)
  980. return wb->qual - wa->qual;
  981. return wb->level - wa->level;
  982. }
  983. #endif /* CONFIG_WPS */
  984. static void dump_scan_res(struct wpa_scan_results *scan_res)
  985. {
  986. #ifndef CONFIG_NO_STDOUT_DEBUG
  987. size_t i;
  988. if (scan_res->res == NULL || scan_res->num == 0)
  989. return;
  990. wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
  991. for (i = 0; i < scan_res->num; i++) {
  992. struct wpa_scan_res *r = scan_res->res[i];
  993. if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
  994. == WPA_SCAN_LEVEL_DBM) {
  995. int snr = r->level - r->noise;
  996. wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
  997. "noise=%d level=%d snr=%d%s flags=0x%x",
  998. MAC2STR(r->bssid), r->freq, r->qual,
  999. r->noise, r->level, snr,
  1000. snr >= GREAT_SNR ? "*" : "", r->flags);
  1001. } else {
  1002. wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
  1003. "noise=%d level=%d flags=0x%x",
  1004. MAC2STR(r->bssid), r->freq, r->qual,
  1005. r->noise, r->level, r->flags);
  1006. }
  1007. }
  1008. #endif /* CONFIG_NO_STDOUT_DEBUG */
  1009. }
  1010. /**
  1011. * wpa_supplicant_get_scan_results - Get scan results
  1012. * @wpa_s: Pointer to wpa_supplicant data
  1013. * @info: Information about what was scanned or %NULL if not available
  1014. * @new_scan: Whether a new scan was performed
  1015. * Returns: Scan results, %NULL on failure
  1016. *
  1017. * This function request the current scan results from the driver and updates
  1018. * the local BSS list wpa_s->bss. The caller is responsible for freeing the
  1019. * results with wpa_scan_results_free().
  1020. */
  1021. struct wpa_scan_results *
  1022. wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
  1023. struct scan_info *info, int new_scan)
  1024. {
  1025. struct wpa_scan_results *scan_res;
  1026. size_t i;
  1027. int (*compar)(const void *, const void *) = wpa_scan_result_compar;
  1028. scan_res = wpa_drv_get_scan_results2(wpa_s);
  1029. if (scan_res == NULL) {
  1030. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
  1031. return NULL;
  1032. }
  1033. #ifdef CONFIG_WPS
  1034. if (wpas_wps_in_progress(wpa_s)) {
  1035. wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
  1036. "provisioning rules");
  1037. compar = wpa_scan_result_wps_compar;
  1038. }
  1039. #endif /* CONFIG_WPS */
  1040. qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
  1041. compar);
  1042. dump_scan_res(scan_res);
  1043. wpa_bss_update_start(wpa_s);
  1044. for (i = 0; i < scan_res->num; i++)
  1045. wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
  1046. wpa_bss_update_end(wpa_s, info, new_scan);
  1047. return scan_res;
  1048. }
  1049. int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
  1050. {
  1051. struct wpa_scan_results *scan_res;
  1052. scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
  1053. if (scan_res == NULL)
  1054. return -1;
  1055. wpa_scan_results_free(scan_res);
  1056. return 0;
  1057. }