scan.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  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_s->prev_scan_wildcard = 0;
  444. wpa_supplicant_assoc_try(wpa_s, ssid);
  445. return;
  446. } else if (wpa_s->conf->ap_scan == 2) {
  447. /*
  448. * User-initiated scan request in ap_scan == 2; scan with
  449. * wildcard SSID.
  450. */
  451. ssid = NULL;
  452. } else {
  453. struct wpa_ssid *start = ssid, *tssid;
  454. int freqs_set = 0;
  455. if (ssid == NULL && max_ssids > 1)
  456. ssid = wpa_s->conf->ssid;
  457. while (ssid) {
  458. if (!ssid->disabled && ssid->scan_ssid) {
  459. wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
  460. ssid->ssid, ssid->ssid_len);
  461. params.ssids[params.num_ssids].ssid =
  462. ssid->ssid;
  463. params.ssids[params.num_ssids].ssid_len =
  464. ssid->ssid_len;
  465. params.num_ssids++;
  466. if (params.num_ssids + 1 >= max_ssids)
  467. break;
  468. }
  469. ssid = ssid->next;
  470. if (ssid == start)
  471. break;
  472. if (ssid == NULL && max_ssids > 1 &&
  473. start != wpa_s->conf->ssid)
  474. ssid = wpa_s->conf->ssid;
  475. }
  476. for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
  477. if (tssid->disabled)
  478. continue;
  479. if ((params.freqs || !freqs_set) && tssid->scan_freq) {
  480. int_array_concat(&params.freqs,
  481. tssid->scan_freq);
  482. } else {
  483. os_free(params.freqs);
  484. params.freqs = NULL;
  485. }
  486. freqs_set = 1;
  487. }
  488. int_array_sort_unique(params.freqs);
  489. }
  490. if (ssid && max_ssids == 1) {
  491. /*
  492. * If the driver is limited to 1 SSID at a time interleave
  493. * wildcard SSID scans with specific SSID scans to avoid
  494. * waiting a long time for a wildcard scan.
  495. */
  496. if (!wpa_s->prev_scan_wildcard) {
  497. params.ssids[0].ssid = NULL;
  498. params.ssids[0].ssid_len = 0;
  499. wpa_s->prev_scan_wildcard = 1;
  500. wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
  501. "wildcard SSID (Interleave with specific)");
  502. } else {
  503. wpa_s->prev_scan_ssid = ssid;
  504. wpa_s->prev_scan_wildcard = 0;
  505. wpa_dbg(wpa_s, MSG_DEBUG,
  506. "Starting AP scan for specific SSID: %s",
  507. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  508. }
  509. } else if (ssid) {
  510. /* max_ssids > 1 */
  511. wpa_s->prev_scan_ssid = ssid;
  512. wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
  513. "the scan request");
  514. params.num_ssids++;
  515. } else {
  516. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  517. params.num_ssids++;
  518. wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
  519. "SSID");
  520. }
  521. wpa_supplicant_optimize_freqs(wpa_s, &params);
  522. extra_ie = wpa_supplicant_extra_ies(wpa_s, &params);
  523. if (params.freqs == NULL && wpa_s->next_scan_freqs) {
  524. wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
  525. "generated frequency list");
  526. params.freqs = wpa_s->next_scan_freqs;
  527. } else
  528. os_free(wpa_s->next_scan_freqs);
  529. wpa_s->next_scan_freqs = NULL;
  530. params.filter_ssids = wpa_supplicant_build_filter_ssids(
  531. wpa_s->conf, &params.num_filter_ssids);
  532. if (extra_ie) {
  533. params.extra_ies = wpabuf_head(extra_ie);
  534. params.extra_ies_len = wpabuf_len(extra_ie);
  535. }
  536. #ifdef CONFIG_P2P
  537. if (wpa_s->p2p_in_provisioning) {
  538. /*
  539. * The interface may not yet be in P2P mode, so we have to
  540. * explicitly request P2P probe to disable CCK rates.
  541. */
  542. params.p2p_probe = 1;
  543. }
  544. #endif /* CONFIG_P2P */
  545. ret = wpa_supplicant_trigger_scan(wpa_s, &params);
  546. wpabuf_free(extra_ie);
  547. os_free(params.freqs);
  548. os_free(params.filter_ssids);
  549. if (ret) {
  550. wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
  551. if (prev_state != wpa_s->wpa_state)
  552. wpa_supplicant_set_state(wpa_s, prev_state);
  553. wpa_supplicant_req_scan(wpa_s, 1, 0);
  554. }
  555. }
  556. /**
  557. * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
  558. * @wpa_s: Pointer to wpa_supplicant data
  559. * @sec: Number of seconds after which to scan
  560. * @usec: Number of microseconds after which to scan
  561. *
  562. * This function is used to schedule a scan for neighboring access points after
  563. * the specified time.
  564. */
  565. void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
  566. {
  567. /* If there's at least one network that should be specifically scanned
  568. * then don't cancel the scan and reschedule. Some drivers do
  569. * background scanning which generates frequent scan results, and that
  570. * causes the specific SSID scan to get continually pushed back and
  571. * never happen, which causes hidden APs to never get probe-scanned.
  572. */
  573. if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
  574. wpa_s->conf->ap_scan == 1) {
  575. struct wpa_ssid *ssid = wpa_s->conf->ssid;
  576. while (ssid) {
  577. if (!ssid->disabled && ssid->scan_ssid)
  578. break;
  579. ssid = ssid->next;
  580. }
  581. if (ssid) {
  582. wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
  583. "ensure that specific SSID scans occur");
  584. return;
  585. }
  586. }
  587. wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
  588. sec, usec);
  589. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  590. eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
  591. }
  592. /**
  593. * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
  594. * @wpa_s: Pointer to wpa_supplicant data
  595. * @sec: Number of seconds after which to scan
  596. * @usec: Number of microseconds after which to scan
  597. *
  598. * This function is used to schedule periodic scans for neighboring
  599. * access points after the specified time.
  600. */
  601. int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
  602. int sec, int usec)
  603. {
  604. if (!wpa_s->sched_scan_supported)
  605. return -1;
  606. eloop_register_timeout(sec, usec,
  607. wpa_supplicant_delayed_sched_scan_timeout,
  608. wpa_s, NULL);
  609. return 0;
  610. }
  611. /**
  612. * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
  613. * @wpa_s: Pointer to wpa_supplicant data
  614. *
  615. * This function is used to schedule periodic scans for neighboring
  616. * access points repeating the scan continuously.
  617. */
  618. int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
  619. {
  620. struct wpa_driver_scan_params params;
  621. enum wpa_states prev_state;
  622. struct wpa_ssid *ssid;
  623. struct wpabuf *wps_ie = NULL;
  624. int ret;
  625. unsigned int max_sched_scan_ssids;
  626. int wildcard = 0;
  627. int need_ssids;
  628. if (!wpa_s->sched_scan_supported)
  629. return -1;
  630. if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
  631. max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
  632. else
  633. max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
  634. if (max_sched_scan_ssids < 1)
  635. return -1;
  636. if (wpa_s->sched_scanning) {
  637. wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
  638. return 0;
  639. }
  640. need_ssids = 0;
  641. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  642. if (!ssid->disabled && !ssid->scan_ssid) {
  643. /* Use wildcard SSID to find this network */
  644. wildcard = 1;
  645. } else if (!ssid->disabled && ssid->ssid_len)
  646. need_ssids++;
  647. }
  648. if (wildcard)
  649. need_ssids++;
  650. if (wpa_s->normal_scans < 3 &&
  651. (need_ssids <= wpa_s->max_scan_ssids ||
  652. wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
  653. /*
  654. * When normal scan can speed up operations, use that for the
  655. * first operations before starting the sched_scan to allow
  656. * user space sleep more. We do this only if the normal scan
  657. * has functionality that is suitable for this or if the
  658. * sched_scan does not have better support for multiple SSIDs.
  659. */
  660. wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
  661. "sched_scan for initial scans (normal_scans=%d)",
  662. wpa_s->normal_scans);
  663. return -1;
  664. }
  665. os_memset(&params, 0, sizeof(params));
  666. /* If we can't allocate space for the filters, we just don't filter */
  667. params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
  668. sizeof(struct wpa_driver_scan_filter));
  669. prev_state = wpa_s->wpa_state;
  670. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  671. wpa_s->wpa_state == WPA_INACTIVE)
  672. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  673. /* Find the starting point from which to continue scanning */
  674. ssid = wpa_s->conf->ssid;
  675. if (wpa_s->prev_sched_ssid) {
  676. while (ssid) {
  677. if (ssid == wpa_s->prev_sched_ssid) {
  678. ssid = ssid->next;
  679. break;
  680. }
  681. ssid = ssid->next;
  682. }
  683. }
  684. if (!ssid || !wpa_s->prev_sched_ssid) {
  685. wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
  686. wpa_s->sched_scan_interval = 10;
  687. wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
  688. wpa_s->first_sched_scan = 1;
  689. ssid = wpa_s->conf->ssid;
  690. wpa_s->prev_sched_ssid = ssid;
  691. }
  692. if (wildcard) {
  693. wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
  694. params.num_ssids++;
  695. }
  696. while (ssid) {
  697. if (ssid->disabled)
  698. goto next;
  699. if (params.num_filter_ssids < wpa_s->max_match_sets &&
  700. params.filter_ssids && ssid->ssid && ssid->ssid_len) {
  701. wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
  702. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  703. os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
  704. ssid->ssid, ssid->ssid_len);
  705. params.filter_ssids[params.num_filter_ssids].ssid_len =
  706. ssid->ssid_len;
  707. params.num_filter_ssids++;
  708. } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
  709. {
  710. wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
  711. "filter for sched_scan - drop filter");
  712. os_free(params.filter_ssids);
  713. params.filter_ssids = NULL;
  714. params.num_filter_ssids = 0;
  715. }
  716. if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
  717. if (params.num_ssids == max_sched_scan_ssids)
  718. break; /* only room for broadcast SSID */
  719. wpa_dbg(wpa_s, MSG_DEBUG,
  720. "add to active scan ssid: %s",
  721. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  722. params.ssids[params.num_ssids].ssid =
  723. ssid->ssid;
  724. params.ssids[params.num_ssids].ssid_len =
  725. ssid->ssid_len;
  726. params.num_ssids++;
  727. if (params.num_ssids >= max_sched_scan_ssids) {
  728. wpa_s->prev_sched_ssid = ssid;
  729. break;
  730. }
  731. }
  732. next:
  733. wpa_s->prev_sched_ssid = ssid;
  734. ssid = ssid->next;
  735. }
  736. if (params.num_filter_ssids == 0) {
  737. os_free(params.filter_ssids);
  738. params.filter_ssids = NULL;
  739. }
  740. if (wpa_s->wps)
  741. wps_ie = wpa_supplicant_extra_ies(wpa_s, &params);
  742. if (ssid || !wpa_s->first_sched_scan) {
  743. wpa_dbg(wpa_s, MSG_DEBUG,
  744. "Starting sched scan: interval %d (no timeout)",
  745. wpa_s->sched_scan_interval);
  746. } else {
  747. wpa_dbg(wpa_s, MSG_DEBUG,
  748. "Starting sched scan: interval %d timeout %d",
  749. wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
  750. }
  751. ret = wpa_supplicant_start_sched_scan(wpa_s, &params,
  752. wpa_s->sched_scan_interval);
  753. wpabuf_free(wps_ie);
  754. os_free(params.filter_ssids);
  755. if (ret) {
  756. wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
  757. if (prev_state != wpa_s->wpa_state)
  758. wpa_supplicant_set_state(wpa_s, prev_state);
  759. return ret;
  760. }
  761. /* If we have more SSIDs to scan, add a timeout so we scan them too */
  762. if (ssid || !wpa_s->first_sched_scan) {
  763. wpa_s->sched_scan_timed_out = 0;
  764. eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
  765. wpa_supplicant_sched_scan_timeout,
  766. wpa_s, NULL);
  767. wpa_s->first_sched_scan = 0;
  768. wpa_s->sched_scan_timeout /= 2;
  769. wpa_s->sched_scan_interval *= 2;
  770. }
  771. return 0;
  772. }
  773. /**
  774. * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
  775. * @wpa_s: Pointer to wpa_supplicant data
  776. *
  777. * This function is used to cancel a scan request scheduled with
  778. * wpa_supplicant_req_scan().
  779. */
  780. void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
  781. {
  782. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
  783. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  784. }
  785. /**
  786. * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
  787. * @wpa_s: Pointer to wpa_supplicant data
  788. *
  789. * This function is used to stop a periodic scheduled scan.
  790. */
  791. void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
  792. {
  793. if (!wpa_s->sched_scanning)
  794. return;
  795. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
  796. eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
  797. wpa_supplicant_stop_sched_scan(wpa_s);
  798. }
  799. void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
  800. int scanning)
  801. {
  802. if (wpa_s->scanning != scanning) {
  803. wpa_s->scanning = scanning;
  804. wpas_notify_scanning(wpa_s);
  805. }
  806. }
  807. static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
  808. {
  809. int rate = 0;
  810. const u8 *ie;
  811. int i;
  812. ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
  813. for (i = 0; ie && i < ie[1]; i++) {
  814. if ((ie[i + 2] & 0x7f) > rate)
  815. rate = ie[i + 2] & 0x7f;
  816. }
  817. ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
  818. for (i = 0; ie && i < ie[1]; i++) {
  819. if ((ie[i + 2] & 0x7f) > rate)
  820. rate = ie[i + 2] & 0x7f;
  821. }
  822. return rate;
  823. }
  824. const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
  825. {
  826. const u8 *end, *pos;
  827. pos = (const u8 *) (res + 1);
  828. end = pos + res->ie_len;
  829. while (pos + 1 < end) {
  830. if (pos + 2 + pos[1] > end)
  831. break;
  832. if (pos[0] == ie)
  833. return pos;
  834. pos += 2 + pos[1];
  835. }
  836. return NULL;
  837. }
  838. const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
  839. u32 vendor_type)
  840. {
  841. const u8 *end, *pos;
  842. pos = (const u8 *) (res + 1);
  843. end = pos + res->ie_len;
  844. while (pos + 1 < end) {
  845. if (pos + 2 + pos[1] > end)
  846. break;
  847. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  848. vendor_type == WPA_GET_BE32(&pos[2]))
  849. return pos;
  850. pos += 2 + pos[1];
  851. }
  852. return NULL;
  853. }
  854. struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
  855. u32 vendor_type)
  856. {
  857. struct wpabuf *buf;
  858. const u8 *end, *pos;
  859. buf = wpabuf_alloc(res->ie_len);
  860. if (buf == NULL)
  861. return NULL;
  862. pos = (const u8 *) (res + 1);
  863. end = pos + res->ie_len;
  864. while (pos + 1 < end) {
  865. if (pos + 2 + pos[1] > end)
  866. break;
  867. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  868. vendor_type == WPA_GET_BE32(&pos[2]))
  869. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  870. pos += 2 + pos[1];
  871. }
  872. if (wpabuf_len(buf) == 0) {
  873. wpabuf_free(buf);
  874. buf = NULL;
  875. }
  876. return buf;
  877. }
  878. struct wpabuf * wpa_scan_get_vendor_ie_multi_beacon(
  879. const struct wpa_scan_res *res, u32 vendor_type)
  880. {
  881. struct wpabuf *buf;
  882. const u8 *end, *pos;
  883. if (res->beacon_ie_len == 0)
  884. return NULL;
  885. buf = wpabuf_alloc(res->beacon_ie_len);
  886. if (buf == NULL)
  887. return NULL;
  888. pos = (const u8 *) (res + 1);
  889. pos += res->ie_len;
  890. end = pos + res->beacon_ie_len;
  891. while (pos + 1 < end) {
  892. if (pos + 2 + pos[1] > end)
  893. break;
  894. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  895. vendor_type == WPA_GET_BE32(&pos[2]))
  896. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  897. pos += 2 + pos[1];
  898. }
  899. if (wpabuf_len(buf) == 0) {
  900. wpabuf_free(buf);
  901. buf = NULL;
  902. }
  903. return buf;
  904. }
  905. /*
  906. * Channels with a great SNR can operate at full rate. What is a great SNR?
  907. * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
  908. * rule of thumb is that any SNR above 20 is good." This one
  909. * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
  910. * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
  911. * conservative value.
  912. */
  913. #define GREAT_SNR 30
  914. /* Compare function for sorting scan results. Return >0 if @b is considered
  915. * better. */
  916. static int wpa_scan_result_compar(const void *a, const void *b)
  917. {
  918. #define IS_5GHZ(n) (n > 4000)
  919. #define MIN(a,b) a < b ? a : b
  920. struct wpa_scan_res **_wa = (void *) a;
  921. struct wpa_scan_res **_wb = (void *) b;
  922. struct wpa_scan_res *wa = *_wa;
  923. struct wpa_scan_res *wb = *_wb;
  924. int wpa_a, wpa_b, maxrate_a, maxrate_b;
  925. int snr_a, snr_b;
  926. /* WPA/WPA2 support preferred */
  927. wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
  928. wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
  929. wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
  930. wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
  931. if (wpa_b && !wpa_a)
  932. return 1;
  933. if (!wpa_b && wpa_a)
  934. return -1;
  935. /* privacy support preferred */
  936. if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
  937. (wb->caps & IEEE80211_CAP_PRIVACY))
  938. return 1;
  939. if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
  940. (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
  941. return -1;
  942. if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
  943. !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
  944. snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
  945. snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
  946. } else {
  947. /* Not suitable information to calculate SNR, so use level */
  948. snr_a = wa->level;
  949. snr_b = wb->level;
  950. }
  951. /* best/max rate preferred if SNR close enough */
  952. if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
  953. (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
  954. maxrate_a = wpa_scan_get_max_rate(wa);
  955. maxrate_b = wpa_scan_get_max_rate(wb);
  956. if (maxrate_a != maxrate_b)
  957. return maxrate_b - maxrate_a;
  958. if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
  959. return IS_5GHZ(wa->freq) ? -1 : 1;
  960. }
  961. /* use freq for channel preference */
  962. /* all things being equal, use SNR; if SNRs are
  963. * identical, use quality values since some drivers may only report
  964. * that value and leave the signal level zero */
  965. if (snr_b == snr_a)
  966. return wb->qual - wa->qual;
  967. return snr_b - snr_a;
  968. #undef MIN
  969. #undef IS_5GHZ
  970. }
  971. #ifdef CONFIG_WPS
  972. /* Compare function for sorting scan results when searching a WPS AP for
  973. * provisioning. Return >0 if @b is considered better. */
  974. static int wpa_scan_result_wps_compar(const void *a, const void *b)
  975. {
  976. struct wpa_scan_res **_wa = (void *) a;
  977. struct wpa_scan_res **_wb = (void *) b;
  978. struct wpa_scan_res *wa = *_wa;
  979. struct wpa_scan_res *wb = *_wb;
  980. int uses_wps_a, uses_wps_b;
  981. struct wpabuf *wps_a, *wps_b;
  982. int res;
  983. /* Optimization - check WPS IE existence before allocated memory and
  984. * doing full reassembly. */
  985. uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
  986. uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
  987. if (uses_wps_a && !uses_wps_b)
  988. return -1;
  989. if (!uses_wps_a && uses_wps_b)
  990. return 1;
  991. if (uses_wps_a && uses_wps_b) {
  992. wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
  993. wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
  994. res = wps_ap_priority_compar(wps_a, wps_b);
  995. wpabuf_free(wps_a);
  996. wpabuf_free(wps_b);
  997. if (res)
  998. return res;
  999. }
  1000. /*
  1001. * Do not use current AP security policy as a sorting criteria during
  1002. * WPS provisioning step since the AP may get reconfigured at the
  1003. * completion of provisioning.
  1004. */
  1005. /* all things being equal, use signal level; if signal levels are
  1006. * identical, use quality values since some drivers may only report
  1007. * that value and leave the signal level zero */
  1008. if (wb->level == wa->level)
  1009. return wb->qual - wa->qual;
  1010. return wb->level - wa->level;
  1011. }
  1012. #endif /* CONFIG_WPS */
  1013. static void dump_scan_res(struct wpa_scan_results *scan_res)
  1014. {
  1015. #ifndef CONFIG_NO_STDOUT_DEBUG
  1016. size_t i;
  1017. if (scan_res->res == NULL || scan_res->num == 0)
  1018. return;
  1019. wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
  1020. for (i = 0; i < scan_res->num; i++) {
  1021. struct wpa_scan_res *r = scan_res->res[i];
  1022. if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
  1023. == WPA_SCAN_LEVEL_DBM) {
  1024. int snr = r->level - r->noise;
  1025. wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
  1026. "noise=%d level=%d snr=%d%s flags=0x%x",
  1027. MAC2STR(r->bssid), r->freq, r->qual,
  1028. r->noise, r->level, snr,
  1029. snr >= GREAT_SNR ? "*" : "", r->flags);
  1030. } else {
  1031. wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
  1032. "noise=%d level=%d flags=0x%x",
  1033. MAC2STR(r->bssid), r->freq, r->qual,
  1034. r->noise, r->level, r->flags);
  1035. }
  1036. }
  1037. #endif /* CONFIG_NO_STDOUT_DEBUG */
  1038. }
  1039. /**
  1040. * wpa_supplicant_get_scan_results - Get scan results
  1041. * @wpa_s: Pointer to wpa_supplicant data
  1042. * @info: Information about what was scanned or %NULL if not available
  1043. * @new_scan: Whether a new scan was performed
  1044. * Returns: Scan results, %NULL on failure
  1045. *
  1046. * This function request the current scan results from the driver and updates
  1047. * the local BSS list wpa_s->bss. The caller is responsible for freeing the
  1048. * results with wpa_scan_results_free().
  1049. */
  1050. struct wpa_scan_results *
  1051. wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
  1052. struct scan_info *info, int new_scan)
  1053. {
  1054. struct wpa_scan_results *scan_res;
  1055. size_t i;
  1056. int (*compar)(const void *, const void *) = wpa_scan_result_compar;
  1057. scan_res = wpa_drv_get_scan_results2(wpa_s);
  1058. if (scan_res == NULL) {
  1059. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
  1060. return NULL;
  1061. }
  1062. #ifdef CONFIG_WPS
  1063. if (wpas_wps_in_progress(wpa_s)) {
  1064. wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
  1065. "provisioning rules");
  1066. compar = wpa_scan_result_wps_compar;
  1067. }
  1068. #endif /* CONFIG_WPS */
  1069. qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
  1070. compar);
  1071. dump_scan_res(scan_res);
  1072. wpa_bss_update_start(wpa_s);
  1073. for (i = 0; i < scan_res->num; i++)
  1074. wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
  1075. wpa_bss_update_end(wpa_s, info, new_scan);
  1076. return scan_res;
  1077. }
  1078. int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
  1079. {
  1080. struct wpa_scan_results *scan_res;
  1081. scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
  1082. if (scan_res == NULL)
  1083. return -1;
  1084. wpa_scan_results_free(scan_res);
  1085. return 0;
  1086. }