scan.c 32 KB

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