scan.c 32 KB

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