scan.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825
  1. /*
  2. * WPA Supplicant - Scanning
  3. * Copyright (c) 2003-2014, 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 "common/wpa_ctrl.h"
  13. #include "config.h"
  14. #include "wpa_supplicant_i.h"
  15. #include "driver_i.h"
  16. #include "wps_supplicant.h"
  17. #include "p2p_supplicant.h"
  18. #include "p2p/p2p.h"
  19. #include "hs20_supplicant.h"
  20. #include "notify.h"
  21. #include "bss.h"
  22. #include "gas_query.h"
  23. #include "scan.h"
  24. static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
  25. {
  26. struct wpa_ssid *ssid;
  27. union wpa_event_data data;
  28. ssid = wpa_supplicant_get_ssid(wpa_s);
  29. if (ssid == NULL)
  30. return;
  31. if (wpa_s->current_ssid == NULL) {
  32. wpa_s->current_ssid = ssid;
  33. if (wpa_s->current_ssid != NULL)
  34. wpas_notify_network_changed(wpa_s);
  35. }
  36. wpa_supplicant_initiate_eapol(wpa_s);
  37. wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured "
  38. "network - generating associated event");
  39. os_memset(&data, 0, sizeof(data));
  40. wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
  41. }
  42. #ifdef CONFIG_WPS
  43. static int wpas_wps_in_use(struct wpa_supplicant *wpa_s,
  44. enum wps_request_type *req_type)
  45. {
  46. struct wpa_ssid *ssid;
  47. int wps = 0;
  48. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  49. if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
  50. continue;
  51. wps = 1;
  52. *req_type = wpas_wps_get_req_type(ssid);
  53. if (!ssid->eap.phase1)
  54. continue;
  55. if (os_strstr(ssid->eap.phase1, "pbc=1"))
  56. return 2;
  57. }
  58. #ifdef CONFIG_P2P
  59. if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p &&
  60. !wpa_s->conf->p2p_disabled) {
  61. wpa_s->wps->dev.p2p = 1;
  62. if (!wps) {
  63. wps = 1;
  64. *req_type = WPS_REQ_ENROLLEE_INFO;
  65. }
  66. }
  67. #endif /* CONFIG_P2P */
  68. return wps;
  69. }
  70. #endif /* CONFIG_WPS */
  71. /**
  72. * wpa_supplicant_enabled_networks - Check whether there are enabled networks
  73. * @wpa_s: Pointer to wpa_supplicant data
  74. * Returns: 0 if no networks are enabled, >0 if networks are enabled
  75. *
  76. * This function is used to figure out whether any networks (or Interworking
  77. * with enabled credentials and auto_interworking) are present in the current
  78. * configuration.
  79. */
  80. int wpa_supplicant_enabled_networks(struct wpa_supplicant *wpa_s)
  81. {
  82. struct wpa_ssid *ssid = wpa_s->conf->ssid;
  83. int count = 0, disabled = 0;
  84. while (ssid) {
  85. if (!wpas_network_disabled(wpa_s, ssid))
  86. count++;
  87. else
  88. disabled++;
  89. ssid = ssid->next;
  90. }
  91. if (wpa_s->conf->cred && wpa_s->conf->interworking &&
  92. wpa_s->conf->auto_interworking)
  93. count++;
  94. if (count == 0 && disabled > 0) {
  95. wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks (%d disabled "
  96. "networks)", disabled);
  97. }
  98. return count;
  99. }
  100. static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
  101. struct wpa_ssid *ssid)
  102. {
  103. while (ssid) {
  104. if (!wpas_network_disabled(wpa_s, ssid))
  105. break;
  106. ssid = ssid->next;
  107. }
  108. /* ap_scan=2 mode - try to associate with each SSID. */
  109. if (ssid == NULL) {
  110. wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
  111. "end of scan list - go back to beginning");
  112. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  113. wpa_supplicant_req_scan(wpa_s, 0, 0);
  114. return;
  115. }
  116. if (ssid->next) {
  117. /* Continue from the next SSID on the next attempt. */
  118. wpa_s->prev_scan_ssid = ssid;
  119. } else {
  120. /* Start from the beginning of the SSID list. */
  121. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  122. }
  123. wpa_supplicant_associate(wpa_s, NULL, ssid);
  124. }
  125. static void wpas_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
  126. {
  127. struct wpa_supplicant *wpa_s = work->wpa_s;
  128. struct wpa_driver_scan_params *params = work->ctx;
  129. int ret;
  130. if (deinit) {
  131. wpa_scan_free_params(params);
  132. return;
  133. }
  134. wpa_supplicant_notify_scanning(wpa_s, 1);
  135. if (wpa_s->clear_driver_scan_cache)
  136. params->only_new_results = 1;
  137. ret = wpa_drv_scan(wpa_s, params);
  138. wpa_scan_free_params(params);
  139. work->ctx = NULL;
  140. if (ret) {
  141. wpa_supplicant_notify_scanning(wpa_s, 0);
  142. wpas_notify_scan_done(wpa_s, 0);
  143. radio_work_done(work);
  144. return;
  145. }
  146. os_get_reltime(&wpa_s->scan_trigger_time);
  147. wpa_s->scan_runs++;
  148. wpa_s->normal_scans++;
  149. wpa_s->own_scan_requested = 1;
  150. wpa_s->clear_driver_scan_cache = 0;
  151. wpa_s->scan_work = work;
  152. }
  153. /**
  154. * wpa_supplicant_trigger_scan - Request driver to start a scan
  155. * @wpa_s: Pointer to wpa_supplicant data
  156. * @params: Scan parameters
  157. * Returns: 0 on success, -1 on failure
  158. */
  159. int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
  160. struct wpa_driver_scan_params *params)
  161. {
  162. struct wpa_driver_scan_params *ctx;
  163. if (wpa_s->scan_work) {
  164. wpa_dbg(wpa_s, MSG_INFO, "Reject scan trigger since one is already pending");
  165. return -1;
  166. }
  167. ctx = wpa_scan_clone_params(params);
  168. if (ctx == NULL)
  169. return -1;
  170. if (radio_add_work(wpa_s, 0, "scan", 0, wpas_trigger_scan_cb, ctx) < 0)
  171. {
  172. wpa_scan_free_params(ctx);
  173. return -1;
  174. }
  175. return 0;
  176. }
  177. static void
  178. wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  179. {
  180. struct wpa_supplicant *wpa_s = eloop_ctx;
  181. wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
  182. if (wpa_supplicant_req_sched_scan(wpa_s))
  183. wpa_supplicant_req_scan(wpa_s, 0, 0);
  184. }
  185. static void
  186. wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  187. {
  188. struct wpa_supplicant *wpa_s = eloop_ctx;
  189. wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
  190. wpa_s->sched_scan_timed_out = 1;
  191. wpa_supplicant_cancel_sched_scan(wpa_s);
  192. }
  193. int wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
  194. struct wpa_driver_scan_params *params,
  195. int interval)
  196. {
  197. int ret;
  198. wpa_supplicant_notify_scanning(wpa_s, 1);
  199. ret = wpa_drv_sched_scan(wpa_s, params, interval * 1000);
  200. if (ret)
  201. wpa_supplicant_notify_scanning(wpa_s, 0);
  202. else
  203. wpa_s->sched_scanning = 1;
  204. return ret;
  205. }
  206. int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
  207. {
  208. int ret;
  209. ret = wpa_drv_stop_sched_scan(wpa_s);
  210. if (ret) {
  211. wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
  212. /* TODO: what to do if stopping fails? */
  213. return -1;
  214. }
  215. return ret;
  216. }
  217. static struct wpa_driver_scan_filter *
  218. wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
  219. {
  220. struct wpa_driver_scan_filter *ssids;
  221. struct wpa_ssid *ssid;
  222. size_t count;
  223. *num_ssids = 0;
  224. if (!conf->filter_ssids)
  225. return NULL;
  226. for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
  227. if (ssid->ssid && ssid->ssid_len)
  228. count++;
  229. }
  230. if (count == 0)
  231. return NULL;
  232. ssids = os_zalloc(count * sizeof(struct wpa_driver_scan_filter));
  233. if (ssids == NULL)
  234. return NULL;
  235. for (ssid = conf->ssid; ssid; ssid = ssid->next) {
  236. if (!ssid->ssid || !ssid->ssid_len)
  237. continue;
  238. os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
  239. ssids[*num_ssids].ssid_len = ssid->ssid_len;
  240. (*num_ssids)++;
  241. }
  242. return ssids;
  243. }
  244. static void wpa_supplicant_optimize_freqs(
  245. struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
  246. {
  247. #ifdef CONFIG_P2P
  248. if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
  249. wpa_s->go_params) {
  250. /* Optimize provisioning state scan based on GO information */
  251. if (wpa_s->p2p_in_provisioning < 5 &&
  252. wpa_s->go_params->freq > 0) {
  253. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
  254. "preferred frequency %d MHz",
  255. wpa_s->go_params->freq);
  256. params->freqs = os_zalloc(2 * sizeof(int));
  257. if (params->freqs)
  258. params->freqs[0] = wpa_s->go_params->freq;
  259. } else if (wpa_s->p2p_in_provisioning < 8 &&
  260. wpa_s->go_params->freq_list[0]) {
  261. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
  262. "channels");
  263. int_array_concat(&params->freqs,
  264. wpa_s->go_params->freq_list);
  265. if (params->freqs)
  266. int_array_sort_unique(params->freqs);
  267. }
  268. wpa_s->p2p_in_provisioning++;
  269. }
  270. #endif /* CONFIG_P2P */
  271. #ifdef CONFIG_WPS
  272. if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
  273. /*
  274. * Optimize post-provisioning scan based on channel used
  275. * during provisioning.
  276. */
  277. wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
  278. "that was used during provisioning", wpa_s->wps_freq);
  279. params->freqs = os_zalloc(2 * sizeof(int));
  280. if (params->freqs)
  281. params->freqs[0] = wpa_s->wps_freq;
  282. wpa_s->after_wps--;
  283. } else if (wpa_s->after_wps)
  284. wpa_s->after_wps--;
  285. if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
  286. {
  287. /* Optimize provisioning scan based on already known channel */
  288. wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
  289. wpa_s->wps_freq);
  290. params->freqs = os_zalloc(2 * sizeof(int));
  291. if (params->freqs)
  292. params->freqs[0] = wpa_s->wps_freq;
  293. wpa_s->known_wps_freq = 0; /* only do this once */
  294. }
  295. #endif /* CONFIG_WPS */
  296. }
  297. #ifdef CONFIG_INTERWORKING
  298. static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
  299. struct wpabuf *buf)
  300. {
  301. if (wpa_s->conf->interworking == 0)
  302. return;
  303. wpabuf_put_u8(buf, WLAN_EID_EXT_CAPAB);
  304. wpabuf_put_u8(buf, 4);
  305. wpabuf_put_u8(buf, 0x00);
  306. wpabuf_put_u8(buf, 0x00);
  307. wpabuf_put_u8(buf, 0x00);
  308. wpabuf_put_u8(buf, 0x80); /* Bit 31 - Interworking */
  309. wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
  310. wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
  311. 1 + ETH_ALEN);
  312. wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
  313. /* No Venue Info */
  314. if (!is_zero_ether_addr(wpa_s->conf->hessid))
  315. wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
  316. }
  317. #endif /* CONFIG_INTERWORKING */
  318. static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
  319. {
  320. struct wpabuf *extra_ie = NULL;
  321. #ifdef CONFIG_WPS
  322. int wps = 0;
  323. enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
  324. #endif /* CONFIG_WPS */
  325. #ifdef CONFIG_INTERWORKING
  326. if (wpa_s->conf->interworking &&
  327. wpabuf_resize(&extra_ie, 100) == 0)
  328. wpas_add_interworking_elements(wpa_s, extra_ie);
  329. #endif /* CONFIG_INTERWORKING */
  330. #ifdef CONFIG_WPS
  331. wps = wpas_wps_in_use(wpa_s, &req_type);
  332. if (wps) {
  333. struct wpabuf *wps_ie;
  334. wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
  335. DEV_PW_DEFAULT,
  336. &wpa_s->wps->dev,
  337. wpa_s->wps->uuid, req_type,
  338. 0, NULL);
  339. if (wps_ie) {
  340. if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
  341. wpabuf_put_buf(extra_ie, wps_ie);
  342. wpabuf_free(wps_ie);
  343. }
  344. }
  345. #ifdef CONFIG_P2P
  346. if (wps) {
  347. size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  348. if (wpabuf_resize(&extra_ie, ielen) == 0)
  349. wpas_p2p_scan_ie(wpa_s, extra_ie);
  350. }
  351. #endif /* CONFIG_P2P */
  352. #endif /* CONFIG_WPS */
  353. #ifdef CONFIG_HS20
  354. if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 7) == 0)
  355. wpas_hs20_add_indication(extra_ie);
  356. #endif /* CONFIG_HS20 */
  357. return extra_ie;
  358. }
  359. #ifdef CONFIG_P2P
  360. /*
  361. * Check whether there are any enabled networks or credentials that could be
  362. * used for a non-P2P connection.
  363. */
  364. static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
  365. {
  366. struct wpa_ssid *ssid;
  367. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  368. if (wpas_network_disabled(wpa_s, ssid))
  369. continue;
  370. if (!ssid->p2p_group)
  371. return 1;
  372. }
  373. if (wpa_s->conf->cred && wpa_s->conf->interworking &&
  374. wpa_s->conf->auto_interworking)
  375. return 1;
  376. return 0;
  377. }
  378. #endif /* CONFIG_P2P */
  379. static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
  380. u16 num_modes,
  381. enum hostapd_hw_mode mode)
  382. {
  383. u16 i;
  384. for (i = 0; i < num_modes; i++) {
  385. if (modes[i].mode == mode)
  386. return &modes[i];
  387. }
  388. return NULL;
  389. }
  390. static void wpa_setband_scan_freqs_list(struct wpa_supplicant *wpa_s,
  391. enum hostapd_hw_mode band,
  392. struct wpa_driver_scan_params *params)
  393. {
  394. /* Include only supported channels for the specified band */
  395. struct hostapd_hw_modes *mode;
  396. int count, i;
  397. mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band);
  398. if (mode == NULL) {
  399. /* No channels supported in this band - use empty list */
  400. params->freqs = os_zalloc(sizeof(int));
  401. return;
  402. }
  403. params->freqs = os_zalloc((mode->num_channels + 1) * sizeof(int));
  404. if (params->freqs == NULL)
  405. return;
  406. for (count = 0, i = 0; i < mode->num_channels; i++) {
  407. if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
  408. continue;
  409. params->freqs[count++] = mode->channels[i].freq;
  410. }
  411. }
  412. static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
  413. struct wpa_driver_scan_params *params)
  414. {
  415. if (wpa_s->hw.modes == NULL)
  416. return; /* unknown what channels the driver supports */
  417. if (params->freqs)
  418. return; /* already using a limited channel set */
  419. if (wpa_s->setband == WPA_SETBAND_5G)
  420. wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
  421. params);
  422. else if (wpa_s->setband == WPA_SETBAND_2G)
  423. wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G,
  424. params);
  425. }
  426. static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
  427. {
  428. struct wpa_supplicant *wpa_s = eloop_ctx;
  429. struct wpa_ssid *ssid;
  430. int ret;
  431. struct wpabuf *extra_ie = NULL;
  432. struct wpa_driver_scan_params params;
  433. struct wpa_driver_scan_params *scan_params;
  434. size_t max_ssids;
  435. enum wpa_states prev_state;
  436. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  437. wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
  438. wpas_p2p_continue_after_scan(wpa_s);
  439. return;
  440. }
  441. if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
  442. wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
  443. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  444. wpas_p2p_continue_after_scan(wpa_s);
  445. return;
  446. }
  447. if (wpa_s->scanning) {
  448. /*
  449. * If we are already in scanning state, we shall reschedule the
  450. * the incoming scan request.
  451. */
  452. wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req");
  453. wpa_supplicant_req_scan(wpa_s, 1, 0);
  454. return;
  455. }
  456. if (wpa_s->external_scan_running) {
  457. struct os_reltime now, diff;
  458. os_get_reltime(&now);
  459. os_reltime_sub(&now, &wpa_s->scan_start_time, &diff);
  460. if (diff.sec < 30) {
  461. wpa_dbg(wpa_s, MSG_DEBUG, "Externally triggered scan running - Reschedule the incoming scan req");
  462. wpa_supplicant_req_scan(wpa_s, 1, 0);
  463. return;
  464. }
  465. }
  466. if (!wpa_supplicant_enabled_networks(wpa_s) &&
  467. wpa_s->scan_req == NORMAL_SCAN_REQ) {
  468. wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
  469. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  470. wpas_p2p_continue_after_scan(wpa_s);
  471. return;
  472. }
  473. if (wpa_s->conf->ap_scan != 0 &&
  474. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
  475. wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
  476. "overriding ap_scan configuration");
  477. wpa_s->conf->ap_scan = 0;
  478. wpas_notify_ap_scan_changed(wpa_s);
  479. }
  480. if (wpa_s->conf->ap_scan == 0) {
  481. wpa_supplicant_gen_assoc_event(wpa_s);
  482. return;
  483. }
  484. #ifdef CONFIG_P2P
  485. if (wpas_p2p_in_progress(wpa_s) || wpas_wpa_is_in_progress(wpa_s, 0)) {
  486. if (wpa_s->sta_scan_pending &&
  487. wpas_p2p_in_progress(wpa_s) == 2 &&
  488. wpa_s->global->p2p_cb_on_scan_complete) {
  489. wpa_dbg(wpa_s, MSG_DEBUG, "Process pending station "
  490. "mode scan during P2P search");
  491. } else {
  492. wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan "
  493. "while P2P operation is in progress");
  494. wpa_s->sta_scan_pending = 1;
  495. wpa_supplicant_req_scan(wpa_s, 5, 0);
  496. return;
  497. }
  498. }
  499. #endif /* CONFIG_P2P */
  500. #ifdef CONFIG_GAS
  501. if (gas_query_in_progress(wpa_s->gas)) {
  502. wpa_dbg(wpa_s, MSG_DEBUG, "Delay scan while GAS query is in progress");
  503. wpa_supplicant_req_scan(wpa_s, 1, 0);
  504. return;
  505. }
  506. #endif /* CONFIG_GAS */
  507. if (wpa_s->conf->ap_scan == 2)
  508. max_ssids = 1;
  509. else {
  510. max_ssids = wpa_s->max_scan_ssids;
  511. if (max_ssids > WPAS_MAX_SCAN_SSIDS)
  512. max_ssids = WPAS_MAX_SCAN_SSIDS;
  513. }
  514. wpa_s->last_scan_req = wpa_s->scan_req;
  515. wpa_s->scan_req = NORMAL_SCAN_REQ;
  516. os_memset(&params, 0, sizeof(params));
  517. prev_state = wpa_s->wpa_state;
  518. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  519. wpa_s->wpa_state == WPA_INACTIVE)
  520. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  521. /*
  522. * If autoscan has set its own scanning parameters
  523. */
  524. if (wpa_s->autoscan_params != NULL) {
  525. scan_params = wpa_s->autoscan_params;
  526. goto scan;
  527. }
  528. if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
  529. wpa_s->connect_without_scan) {
  530. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  531. if (ssid == wpa_s->connect_without_scan)
  532. break;
  533. }
  534. wpa_s->connect_without_scan = NULL;
  535. if (ssid) {
  536. wpa_printf(MSG_DEBUG, "Start a pre-selected network "
  537. "without scan step");
  538. wpa_supplicant_associate(wpa_s, NULL, ssid);
  539. return;
  540. }
  541. }
  542. #ifdef CONFIG_P2P
  543. if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
  544. wpa_s->go_params) {
  545. wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
  546. wpa_s->p2p_in_provisioning,
  547. wpa_s->show_group_started);
  548. params.ssids[0].ssid = wpa_s->go_params->ssid;
  549. params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
  550. params.num_ssids = 1;
  551. goto ssid_list_set;
  552. }
  553. #endif /* CONFIG_P2P */
  554. /* Find the starting point from which to continue scanning */
  555. ssid = wpa_s->conf->ssid;
  556. if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
  557. while (ssid) {
  558. if (ssid == wpa_s->prev_scan_ssid) {
  559. ssid = ssid->next;
  560. break;
  561. }
  562. ssid = ssid->next;
  563. }
  564. }
  565. if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
  566. wpa_s->conf->ap_scan == 2) {
  567. wpa_s->connect_without_scan = NULL;
  568. wpa_s->prev_scan_wildcard = 0;
  569. wpa_supplicant_assoc_try(wpa_s, ssid);
  570. return;
  571. } else if (wpa_s->conf->ap_scan == 2) {
  572. /*
  573. * User-initiated scan request in ap_scan == 2; scan with
  574. * wildcard SSID.
  575. */
  576. ssid = NULL;
  577. } else {
  578. struct wpa_ssid *start = ssid, *tssid;
  579. int freqs_set = 0;
  580. if (ssid == NULL && max_ssids > 1)
  581. ssid = wpa_s->conf->ssid;
  582. while (ssid) {
  583. if (!wpas_network_disabled(wpa_s, ssid) &&
  584. ssid->scan_ssid) {
  585. wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
  586. ssid->ssid, ssid->ssid_len);
  587. params.ssids[params.num_ssids].ssid =
  588. ssid->ssid;
  589. params.ssids[params.num_ssids].ssid_len =
  590. ssid->ssid_len;
  591. params.num_ssids++;
  592. if (params.num_ssids + 1 >= max_ssids)
  593. break;
  594. }
  595. ssid = ssid->next;
  596. if (ssid == start)
  597. break;
  598. if (ssid == NULL && max_ssids > 1 &&
  599. start != wpa_s->conf->ssid)
  600. ssid = wpa_s->conf->ssid;
  601. }
  602. for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
  603. if (wpas_network_disabled(wpa_s, tssid))
  604. continue;
  605. if ((params.freqs || !freqs_set) && tssid->scan_freq) {
  606. int_array_concat(&params.freqs,
  607. tssid->scan_freq);
  608. } else {
  609. os_free(params.freqs);
  610. params.freqs = NULL;
  611. }
  612. freqs_set = 1;
  613. }
  614. int_array_sort_unique(params.freqs);
  615. }
  616. if (ssid && max_ssids == 1) {
  617. /*
  618. * If the driver is limited to 1 SSID at a time interleave
  619. * wildcard SSID scans with specific SSID scans to avoid
  620. * waiting a long time for a wildcard scan.
  621. */
  622. if (!wpa_s->prev_scan_wildcard) {
  623. params.ssids[0].ssid = NULL;
  624. params.ssids[0].ssid_len = 0;
  625. wpa_s->prev_scan_wildcard = 1;
  626. wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
  627. "wildcard SSID (Interleave with specific)");
  628. } else {
  629. wpa_s->prev_scan_ssid = ssid;
  630. wpa_s->prev_scan_wildcard = 0;
  631. wpa_dbg(wpa_s, MSG_DEBUG,
  632. "Starting AP scan for specific SSID: %s",
  633. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  634. }
  635. } else if (ssid) {
  636. /* max_ssids > 1 */
  637. wpa_s->prev_scan_ssid = ssid;
  638. wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
  639. "the scan request");
  640. params.num_ssids++;
  641. } else if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
  642. wpa_s->manual_scan_passive && params.num_ssids == 0) {
  643. wpa_dbg(wpa_s, MSG_DEBUG, "Use passive scan based on manual request");
  644. } else {
  645. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  646. params.num_ssids++;
  647. wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
  648. "SSID");
  649. }
  650. #ifdef CONFIG_P2P
  651. ssid_list_set:
  652. #endif /* CONFIG_P2P */
  653. wpa_supplicant_optimize_freqs(wpa_s, &params);
  654. extra_ie = wpa_supplicant_extra_ies(wpa_s);
  655. if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
  656. wpa_s->manual_scan_only_new)
  657. params.only_new_results = 1;
  658. if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
  659. wpa_s->manual_scan_freqs) {
  660. wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
  661. params.freqs = wpa_s->manual_scan_freqs;
  662. wpa_s->manual_scan_freqs = NULL;
  663. }
  664. if (params.freqs == NULL && wpa_s->next_scan_freqs) {
  665. wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
  666. "generated frequency list");
  667. params.freqs = wpa_s->next_scan_freqs;
  668. } else
  669. os_free(wpa_s->next_scan_freqs);
  670. wpa_s->next_scan_freqs = NULL;
  671. wpa_setband_scan_freqs(wpa_s, &params);
  672. /* See if user specified frequencies. If so, scan only those. */
  673. if (wpa_s->conf->freq_list && !params.freqs) {
  674. wpa_dbg(wpa_s, MSG_DEBUG,
  675. "Optimize scan based on conf->freq_list");
  676. int_array_concat(&params.freqs, wpa_s->conf->freq_list);
  677. }
  678. /* Use current associated channel? */
  679. if (wpa_s->conf->scan_cur_freq && !params.freqs) {
  680. unsigned int num = wpa_s->num_multichan_concurrent;
  681. params.freqs = os_calloc(num + 1, sizeof(int));
  682. if (params.freqs) {
  683. num = get_shared_radio_freqs(wpa_s, params.freqs, num);
  684. if (num > 0) {
  685. wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
  686. "current operating channels since "
  687. "scan_cur_freq is enabled");
  688. } else {
  689. os_free(params.freqs);
  690. params.freqs = NULL;
  691. }
  692. }
  693. }
  694. params.filter_ssids = wpa_supplicant_build_filter_ssids(
  695. wpa_s->conf, &params.num_filter_ssids);
  696. if (extra_ie) {
  697. params.extra_ies = wpabuf_head(extra_ie);
  698. params.extra_ies_len = wpabuf_len(extra_ie);
  699. }
  700. #ifdef CONFIG_P2P
  701. if (wpa_s->p2p_in_provisioning ||
  702. (wpa_s->show_group_started && wpa_s->go_params)) {
  703. /*
  704. * The interface may not yet be in P2P mode, so we have to
  705. * explicitly request P2P probe to disable CCK rates.
  706. */
  707. params.p2p_probe = 1;
  708. }
  709. #endif /* CONFIG_P2P */
  710. scan_params = &params;
  711. scan:
  712. #ifdef CONFIG_P2P
  713. /*
  714. * If the driver does not support multi-channel concurrency and a
  715. * virtual interface that shares the same radio with the wpa_s interface
  716. * is operating there may not be need to scan other channels apart from
  717. * the current operating channel on the other virtual interface. Filter
  718. * out other channels in case we are trying to find a connection for a
  719. * station interface when we are not configured to prefer station
  720. * connection and a concurrent operation is already in process.
  721. */
  722. if (wpa_s->scan_for_connection &&
  723. wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
  724. !scan_params->freqs && !params.freqs &&
  725. wpas_is_p2p_prioritized(wpa_s) &&
  726. wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
  727. non_p2p_network_enabled(wpa_s)) {
  728. unsigned int num = wpa_s->num_multichan_concurrent;
  729. params.freqs = os_calloc(num + 1, sizeof(int));
  730. if (params.freqs) {
  731. num = get_shared_radio_freqs(wpa_s, params.freqs, num);
  732. if (num > 0 && num == wpa_s->num_multichan_concurrent) {
  733. wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
  734. } else {
  735. os_free(params.freqs);
  736. params.freqs = NULL;
  737. }
  738. }
  739. }
  740. #endif /* CONFIG_P2P */
  741. ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
  742. if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
  743. !wpa_s->manual_scan_freqs) {
  744. /* Restore manual_scan_freqs for the next attempt */
  745. wpa_s->manual_scan_freqs = params.freqs;
  746. params.freqs = NULL;
  747. }
  748. wpabuf_free(extra_ie);
  749. os_free(params.freqs);
  750. os_free(params.filter_ssids);
  751. if (ret) {
  752. wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
  753. if (prev_state != wpa_s->wpa_state)
  754. wpa_supplicant_set_state(wpa_s, prev_state);
  755. /* Restore scan_req since we will try to scan again */
  756. wpa_s->scan_req = wpa_s->last_scan_req;
  757. wpa_supplicant_req_scan(wpa_s, 1, 0);
  758. } else {
  759. wpa_s->scan_for_connection = 0;
  760. }
  761. }
  762. void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
  763. {
  764. struct os_reltime remaining, new_int;
  765. int cancelled;
  766. cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
  767. &remaining);
  768. new_int.sec = sec;
  769. new_int.usec = 0;
  770. if (cancelled && os_reltime_before(&remaining, &new_int)) {
  771. new_int.sec = remaining.sec;
  772. new_int.usec = remaining.usec;
  773. }
  774. if (cancelled) {
  775. eloop_register_timeout(new_int.sec, new_int.usec,
  776. wpa_supplicant_scan, wpa_s, NULL);
  777. }
  778. wpa_s->scan_interval = sec;
  779. }
  780. /**
  781. * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
  782. * @wpa_s: Pointer to wpa_supplicant data
  783. * @sec: Number of seconds after which to scan
  784. * @usec: Number of microseconds after which to scan
  785. *
  786. * This function is used to schedule a scan for neighboring access points after
  787. * the specified time.
  788. */
  789. void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
  790. {
  791. if (eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL))
  792. {
  793. wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d sec %d usec",
  794. sec, usec);
  795. return;
  796. }
  797. wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
  798. sec, usec);
  799. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  800. eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
  801. }
  802. /**
  803. * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
  804. * @wpa_s: Pointer to wpa_supplicant data
  805. * @sec: Number of seconds after which to scan
  806. * @usec: Number of microseconds after which to scan
  807. * Returns: 0 on success or -1 otherwise
  808. *
  809. * This function is used to schedule periodic scans for neighboring
  810. * access points after the specified time.
  811. */
  812. int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
  813. int sec, int usec)
  814. {
  815. if (!wpa_s->sched_scan_supported)
  816. return -1;
  817. eloop_register_timeout(sec, usec,
  818. wpa_supplicant_delayed_sched_scan_timeout,
  819. wpa_s, NULL);
  820. return 0;
  821. }
  822. /**
  823. * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
  824. * @wpa_s: Pointer to wpa_supplicant data
  825. * Returns: 0 is sched_scan was started or -1 otherwise
  826. *
  827. * This function is used to schedule periodic scans for neighboring
  828. * access points repeating the scan continuously.
  829. */
  830. int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
  831. {
  832. struct wpa_driver_scan_params params;
  833. struct wpa_driver_scan_params *scan_params;
  834. enum wpa_states prev_state;
  835. struct wpa_ssid *ssid = NULL;
  836. struct wpabuf *extra_ie = NULL;
  837. int ret;
  838. unsigned int max_sched_scan_ssids;
  839. int wildcard = 0;
  840. int need_ssids;
  841. if (!wpa_s->sched_scan_supported)
  842. return -1;
  843. if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
  844. max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
  845. else
  846. max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
  847. if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
  848. return -1;
  849. if (wpa_s->sched_scanning) {
  850. wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
  851. return 0;
  852. }
  853. need_ssids = 0;
  854. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  855. if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
  856. /* Use wildcard SSID to find this network */
  857. wildcard = 1;
  858. } else if (!wpas_network_disabled(wpa_s, ssid) &&
  859. ssid->ssid_len)
  860. need_ssids++;
  861. #ifdef CONFIG_WPS
  862. if (!wpas_network_disabled(wpa_s, ssid) &&
  863. ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
  864. /*
  865. * Normal scan is more reliable and faster for WPS
  866. * operations and since these are for short periods of
  867. * time, the benefit of trying to use sched_scan would
  868. * be limited.
  869. */
  870. wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
  871. "sched_scan for WPS");
  872. return -1;
  873. }
  874. #endif /* CONFIG_WPS */
  875. }
  876. if (wildcard)
  877. need_ssids++;
  878. if (wpa_s->normal_scans < 3 &&
  879. (need_ssids <= wpa_s->max_scan_ssids ||
  880. wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
  881. /*
  882. * When normal scan can speed up operations, use that for the
  883. * first operations before starting the sched_scan to allow
  884. * user space sleep more. We do this only if the normal scan
  885. * has functionality that is suitable for this or if the
  886. * sched_scan does not have better support for multiple SSIDs.
  887. */
  888. wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
  889. "sched_scan for initial scans (normal_scans=%d)",
  890. wpa_s->normal_scans);
  891. return -1;
  892. }
  893. os_memset(&params, 0, sizeof(params));
  894. /* If we can't allocate space for the filters, we just don't filter */
  895. params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
  896. sizeof(struct wpa_driver_scan_filter));
  897. prev_state = wpa_s->wpa_state;
  898. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  899. wpa_s->wpa_state == WPA_INACTIVE)
  900. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  901. if (wpa_s->autoscan_params != NULL) {
  902. scan_params = wpa_s->autoscan_params;
  903. goto scan;
  904. }
  905. /* Find the starting point from which to continue scanning */
  906. ssid = wpa_s->conf->ssid;
  907. if (wpa_s->prev_sched_ssid) {
  908. while (ssid) {
  909. if (ssid == wpa_s->prev_sched_ssid) {
  910. ssid = ssid->next;
  911. break;
  912. }
  913. ssid = ssid->next;
  914. }
  915. }
  916. if (!ssid || !wpa_s->prev_sched_ssid) {
  917. wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
  918. if (wpa_s->conf->sched_scan_interval)
  919. wpa_s->sched_scan_interval =
  920. wpa_s->conf->sched_scan_interval;
  921. if (wpa_s->sched_scan_interval == 0)
  922. wpa_s->sched_scan_interval = 10;
  923. wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
  924. wpa_s->first_sched_scan = 1;
  925. ssid = wpa_s->conf->ssid;
  926. wpa_s->prev_sched_ssid = ssid;
  927. }
  928. if (wildcard) {
  929. wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
  930. params.num_ssids++;
  931. }
  932. while (ssid) {
  933. if (wpas_network_disabled(wpa_s, ssid))
  934. goto next;
  935. if (params.num_filter_ssids < wpa_s->max_match_sets &&
  936. params.filter_ssids && ssid->ssid && ssid->ssid_len) {
  937. wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
  938. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  939. os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
  940. ssid->ssid, ssid->ssid_len);
  941. params.filter_ssids[params.num_filter_ssids].ssid_len =
  942. ssid->ssid_len;
  943. params.num_filter_ssids++;
  944. } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
  945. {
  946. wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
  947. "filter for sched_scan - drop filter");
  948. os_free(params.filter_ssids);
  949. params.filter_ssids = NULL;
  950. params.num_filter_ssids = 0;
  951. }
  952. if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
  953. if (params.num_ssids == max_sched_scan_ssids)
  954. break; /* only room for broadcast SSID */
  955. wpa_dbg(wpa_s, MSG_DEBUG,
  956. "add to active scan ssid: %s",
  957. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  958. params.ssids[params.num_ssids].ssid =
  959. ssid->ssid;
  960. params.ssids[params.num_ssids].ssid_len =
  961. ssid->ssid_len;
  962. params.num_ssids++;
  963. if (params.num_ssids >= max_sched_scan_ssids) {
  964. wpa_s->prev_sched_ssid = ssid;
  965. do {
  966. ssid = ssid->next;
  967. } while (ssid &&
  968. (wpas_network_disabled(wpa_s, ssid) ||
  969. !ssid->scan_ssid));
  970. break;
  971. }
  972. }
  973. next:
  974. wpa_s->prev_sched_ssid = ssid;
  975. ssid = ssid->next;
  976. }
  977. if (params.num_filter_ssids == 0) {
  978. os_free(params.filter_ssids);
  979. params.filter_ssids = NULL;
  980. }
  981. extra_ie = wpa_supplicant_extra_ies(wpa_s);
  982. if (extra_ie) {
  983. params.extra_ies = wpabuf_head(extra_ie);
  984. params.extra_ies_len = wpabuf_len(extra_ie);
  985. }
  986. scan_params = &params;
  987. scan:
  988. if (ssid || !wpa_s->first_sched_scan) {
  989. wpa_dbg(wpa_s, MSG_DEBUG,
  990. "Starting sched scan: interval %d timeout %d",
  991. wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
  992. } else {
  993. wpa_dbg(wpa_s, MSG_DEBUG,
  994. "Starting sched scan: interval %d (no timeout)",
  995. wpa_s->sched_scan_interval);
  996. }
  997. wpa_setband_scan_freqs(wpa_s, scan_params);
  998. ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params,
  999. wpa_s->sched_scan_interval);
  1000. wpabuf_free(extra_ie);
  1001. os_free(params.filter_ssids);
  1002. if (ret) {
  1003. wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
  1004. if (prev_state != wpa_s->wpa_state)
  1005. wpa_supplicant_set_state(wpa_s, prev_state);
  1006. return ret;
  1007. }
  1008. /* If we have more SSIDs to scan, add a timeout so we scan them too */
  1009. if (ssid || !wpa_s->first_sched_scan) {
  1010. wpa_s->sched_scan_timed_out = 0;
  1011. eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
  1012. wpa_supplicant_sched_scan_timeout,
  1013. wpa_s, NULL);
  1014. wpa_s->first_sched_scan = 0;
  1015. wpa_s->sched_scan_timeout /= 2;
  1016. wpa_s->sched_scan_interval *= 2;
  1017. if (wpa_s->sched_scan_timeout < wpa_s->sched_scan_interval) {
  1018. wpa_s->sched_scan_interval = 10;
  1019. wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
  1020. }
  1021. }
  1022. /* If there is no more ssids, start next time from the beginning */
  1023. if (!ssid)
  1024. wpa_s->prev_sched_ssid = NULL;
  1025. return 0;
  1026. }
  1027. /**
  1028. * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
  1029. * @wpa_s: Pointer to wpa_supplicant data
  1030. *
  1031. * This function is used to cancel a scan request scheduled with
  1032. * wpa_supplicant_req_scan().
  1033. */
  1034. void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
  1035. {
  1036. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
  1037. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  1038. wpas_p2p_continue_after_scan(wpa_s);
  1039. }
  1040. /**
  1041. * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
  1042. * @wpa_s: Pointer to wpa_supplicant data
  1043. *
  1044. * This function is used to stop a delayed scheduled scan.
  1045. */
  1046. void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
  1047. {
  1048. if (!wpa_s->sched_scan_supported)
  1049. return;
  1050. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
  1051. eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
  1052. wpa_s, NULL);
  1053. }
  1054. /**
  1055. * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
  1056. * @wpa_s: Pointer to wpa_supplicant data
  1057. *
  1058. * This function is used to stop a periodic scheduled scan.
  1059. */
  1060. void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
  1061. {
  1062. if (!wpa_s->sched_scanning)
  1063. return;
  1064. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
  1065. eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
  1066. wpa_supplicant_stop_sched_scan(wpa_s);
  1067. }
  1068. /**
  1069. * wpa_supplicant_notify_scanning - Indicate possible scan state change
  1070. * @wpa_s: Pointer to wpa_supplicant data
  1071. * @scanning: Whether scanning is currently in progress
  1072. *
  1073. * This function is to generate scanning notifycations. It is called whenever
  1074. * there may have been a change in scanning (scan started, completed, stopped).
  1075. * wpas_notify_scanning() is called whenever the scanning state changed from the
  1076. * previously notified state.
  1077. */
  1078. void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
  1079. int scanning)
  1080. {
  1081. if (wpa_s->scanning != scanning) {
  1082. wpa_s->scanning = scanning;
  1083. wpas_notify_scanning(wpa_s);
  1084. }
  1085. }
  1086. static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
  1087. {
  1088. int rate = 0;
  1089. const u8 *ie;
  1090. int i;
  1091. ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
  1092. for (i = 0; ie && i < ie[1]; i++) {
  1093. if ((ie[i + 2] & 0x7f) > rate)
  1094. rate = ie[i + 2] & 0x7f;
  1095. }
  1096. ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
  1097. for (i = 0; ie && i < ie[1]; i++) {
  1098. if ((ie[i + 2] & 0x7f) > rate)
  1099. rate = ie[i + 2] & 0x7f;
  1100. }
  1101. return rate;
  1102. }
  1103. /**
  1104. * wpa_scan_get_ie - Fetch a specified information element from a scan result
  1105. * @res: Scan result entry
  1106. * @ie: Information element identitifier (WLAN_EID_*)
  1107. * Returns: Pointer to the information element (id field) or %NULL if not found
  1108. *
  1109. * This function returns the first matching information element in the scan
  1110. * result.
  1111. */
  1112. const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
  1113. {
  1114. const u8 *end, *pos;
  1115. pos = (const u8 *) (res + 1);
  1116. end = pos + res->ie_len;
  1117. while (pos + 1 < end) {
  1118. if (pos + 2 + pos[1] > end)
  1119. break;
  1120. if (pos[0] == ie)
  1121. return pos;
  1122. pos += 2 + pos[1];
  1123. }
  1124. return NULL;
  1125. }
  1126. /**
  1127. * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
  1128. * @res: Scan result entry
  1129. * @vendor_type: Vendor type (four octets starting the IE payload)
  1130. * Returns: Pointer to the information element (id field) or %NULL if not found
  1131. *
  1132. * This function returns the first matching information element in the scan
  1133. * result.
  1134. */
  1135. const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
  1136. u32 vendor_type)
  1137. {
  1138. const u8 *end, *pos;
  1139. pos = (const u8 *) (res + 1);
  1140. end = pos + res->ie_len;
  1141. while (pos + 1 < end) {
  1142. if (pos + 2 + pos[1] > end)
  1143. break;
  1144. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  1145. vendor_type == WPA_GET_BE32(&pos[2]))
  1146. return pos;
  1147. pos += 2 + pos[1];
  1148. }
  1149. return NULL;
  1150. }
  1151. /**
  1152. * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
  1153. * @res: Scan result entry
  1154. * @vendor_type: Vendor type (four octets starting the IE payload)
  1155. * Returns: Pointer to the information element (id field) or %NULL if not found
  1156. *
  1157. * This function returns the first matching information element in the scan
  1158. * result.
  1159. *
  1160. * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
  1161. * from Beacon frames instead of either Beacon or Probe Response frames.
  1162. */
  1163. const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
  1164. u32 vendor_type)
  1165. {
  1166. const u8 *end, *pos;
  1167. if (res->beacon_ie_len == 0)
  1168. return NULL;
  1169. pos = (const u8 *) (res + 1);
  1170. pos += res->ie_len;
  1171. end = pos + res->beacon_ie_len;
  1172. while (pos + 1 < end) {
  1173. if (pos + 2 + pos[1] > end)
  1174. break;
  1175. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  1176. vendor_type == WPA_GET_BE32(&pos[2]))
  1177. return pos;
  1178. pos += 2 + pos[1];
  1179. }
  1180. return NULL;
  1181. }
  1182. /**
  1183. * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
  1184. * @res: Scan result entry
  1185. * @vendor_type: Vendor type (four octets starting the IE payload)
  1186. * Returns: Pointer to the information element payload or %NULL if not found
  1187. *
  1188. * This function returns concatenated payload of possibly fragmented vendor
  1189. * specific information elements in the scan result. The caller is responsible
  1190. * for freeing the returned buffer.
  1191. */
  1192. struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
  1193. u32 vendor_type)
  1194. {
  1195. struct wpabuf *buf;
  1196. const u8 *end, *pos;
  1197. buf = wpabuf_alloc(res->ie_len);
  1198. if (buf == NULL)
  1199. return NULL;
  1200. pos = (const u8 *) (res + 1);
  1201. end = pos + res->ie_len;
  1202. while (pos + 1 < end) {
  1203. if (pos + 2 + pos[1] > end)
  1204. break;
  1205. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  1206. vendor_type == WPA_GET_BE32(&pos[2]))
  1207. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  1208. pos += 2 + pos[1];
  1209. }
  1210. if (wpabuf_len(buf) == 0) {
  1211. wpabuf_free(buf);
  1212. buf = NULL;
  1213. }
  1214. return buf;
  1215. }
  1216. /*
  1217. * Channels with a great SNR can operate at full rate. What is a great SNR?
  1218. * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
  1219. * rule of thumb is that any SNR above 20 is good." This one
  1220. * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
  1221. * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
  1222. * conservative value.
  1223. */
  1224. #define GREAT_SNR 30
  1225. /* Compare function for sorting scan results. Return >0 if @b is considered
  1226. * better. */
  1227. static int wpa_scan_result_compar(const void *a, const void *b)
  1228. {
  1229. #define IS_5GHZ(n) (n > 4000)
  1230. #define MIN(a,b) a < b ? a : b
  1231. struct wpa_scan_res **_wa = (void *) a;
  1232. struct wpa_scan_res **_wb = (void *) b;
  1233. struct wpa_scan_res *wa = *_wa;
  1234. struct wpa_scan_res *wb = *_wb;
  1235. int wpa_a, wpa_b, maxrate_a, maxrate_b;
  1236. int snr_a, snr_b;
  1237. /* WPA/WPA2 support preferred */
  1238. wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
  1239. wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
  1240. wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
  1241. wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
  1242. if (wpa_b && !wpa_a)
  1243. return 1;
  1244. if (!wpa_b && wpa_a)
  1245. return -1;
  1246. /* privacy support preferred */
  1247. if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
  1248. (wb->caps & IEEE80211_CAP_PRIVACY))
  1249. return 1;
  1250. if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
  1251. (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
  1252. return -1;
  1253. if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
  1254. !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
  1255. snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
  1256. snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
  1257. } else {
  1258. /* Not suitable information to calculate SNR, so use level */
  1259. snr_a = wa->level;
  1260. snr_b = wb->level;
  1261. }
  1262. /* best/max rate preferred if SNR close enough */
  1263. if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
  1264. (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
  1265. maxrate_a = wpa_scan_get_max_rate(wa);
  1266. maxrate_b = wpa_scan_get_max_rate(wb);
  1267. if (maxrate_a != maxrate_b)
  1268. return maxrate_b - maxrate_a;
  1269. if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
  1270. return IS_5GHZ(wa->freq) ? -1 : 1;
  1271. }
  1272. /* use freq for channel preference */
  1273. /* all things being equal, use SNR; if SNRs are
  1274. * identical, use quality values since some drivers may only report
  1275. * that value and leave the signal level zero */
  1276. if (snr_b == snr_a)
  1277. return wb->qual - wa->qual;
  1278. return snr_b - snr_a;
  1279. #undef MIN
  1280. #undef IS_5GHZ
  1281. }
  1282. #ifdef CONFIG_WPS
  1283. /* Compare function for sorting scan results when searching a WPS AP for
  1284. * provisioning. Return >0 if @b is considered better. */
  1285. static int wpa_scan_result_wps_compar(const void *a, const void *b)
  1286. {
  1287. struct wpa_scan_res **_wa = (void *) a;
  1288. struct wpa_scan_res **_wb = (void *) b;
  1289. struct wpa_scan_res *wa = *_wa;
  1290. struct wpa_scan_res *wb = *_wb;
  1291. int uses_wps_a, uses_wps_b;
  1292. struct wpabuf *wps_a, *wps_b;
  1293. int res;
  1294. /* Optimization - check WPS IE existence before allocated memory and
  1295. * doing full reassembly. */
  1296. uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
  1297. uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
  1298. if (uses_wps_a && !uses_wps_b)
  1299. return -1;
  1300. if (!uses_wps_a && uses_wps_b)
  1301. return 1;
  1302. if (uses_wps_a && uses_wps_b) {
  1303. wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
  1304. wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
  1305. res = wps_ap_priority_compar(wps_a, wps_b);
  1306. wpabuf_free(wps_a);
  1307. wpabuf_free(wps_b);
  1308. if (res)
  1309. return res;
  1310. }
  1311. /*
  1312. * Do not use current AP security policy as a sorting criteria during
  1313. * WPS provisioning step since the AP may get reconfigured at the
  1314. * completion of provisioning.
  1315. */
  1316. /* all things being equal, use signal level; if signal levels are
  1317. * identical, use quality values since some drivers may only report
  1318. * that value and leave the signal level zero */
  1319. if (wb->level == wa->level)
  1320. return wb->qual - wa->qual;
  1321. return wb->level - wa->level;
  1322. }
  1323. #endif /* CONFIG_WPS */
  1324. static void dump_scan_res(struct wpa_scan_results *scan_res)
  1325. {
  1326. #ifndef CONFIG_NO_STDOUT_DEBUG
  1327. size_t i;
  1328. if (scan_res->res == NULL || scan_res->num == 0)
  1329. return;
  1330. wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
  1331. for (i = 0; i < scan_res->num; i++) {
  1332. struct wpa_scan_res *r = scan_res->res[i];
  1333. u8 *pos;
  1334. if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
  1335. == WPA_SCAN_LEVEL_DBM) {
  1336. int snr = r->level - r->noise;
  1337. wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
  1338. "noise=%d level=%d snr=%d%s flags=0x%x "
  1339. "age=%u",
  1340. MAC2STR(r->bssid), r->freq, r->qual,
  1341. r->noise, r->level, snr,
  1342. snr >= GREAT_SNR ? "*" : "", r->flags,
  1343. r->age);
  1344. } else {
  1345. wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
  1346. "noise=%d level=%d flags=0x%x age=%u",
  1347. MAC2STR(r->bssid), r->freq, r->qual,
  1348. r->noise, r->level, r->flags, r->age);
  1349. }
  1350. pos = (u8 *) (r + 1);
  1351. if (r->ie_len)
  1352. wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
  1353. pos += r->ie_len;
  1354. if (r->beacon_ie_len)
  1355. wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
  1356. pos, r->beacon_ie_len);
  1357. }
  1358. #endif /* CONFIG_NO_STDOUT_DEBUG */
  1359. }
  1360. /**
  1361. * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
  1362. * @wpa_s: Pointer to wpa_supplicant data
  1363. * @bssid: BSSID to check
  1364. * Returns: 0 if the BSSID is filtered or 1 if not
  1365. *
  1366. * This function is used to filter out specific BSSIDs from scan reslts mainly
  1367. * for testing purposes (SET bssid_filter ctrl_iface command).
  1368. */
  1369. int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
  1370. const u8 *bssid)
  1371. {
  1372. size_t i;
  1373. if (wpa_s->bssid_filter == NULL)
  1374. return 1;
  1375. for (i = 0; i < wpa_s->bssid_filter_count; i++) {
  1376. if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
  1377. ETH_ALEN) == 0)
  1378. return 1;
  1379. }
  1380. return 0;
  1381. }
  1382. static void filter_scan_res(struct wpa_supplicant *wpa_s,
  1383. struct wpa_scan_results *res)
  1384. {
  1385. size_t i, j;
  1386. if (wpa_s->bssid_filter == NULL)
  1387. return;
  1388. for (i = 0, j = 0; i < res->num; i++) {
  1389. if (wpa_supplicant_filter_bssid_match(wpa_s,
  1390. res->res[i]->bssid)) {
  1391. res->res[j++] = res->res[i];
  1392. } else {
  1393. os_free(res->res[i]);
  1394. res->res[i] = NULL;
  1395. }
  1396. }
  1397. if (res->num != j) {
  1398. wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
  1399. (int) (res->num - j));
  1400. res->num = j;
  1401. }
  1402. }
  1403. /**
  1404. * wpa_supplicant_get_scan_results - Get scan results
  1405. * @wpa_s: Pointer to wpa_supplicant data
  1406. * @info: Information about what was scanned or %NULL if not available
  1407. * @new_scan: Whether a new scan was performed
  1408. * Returns: Scan results, %NULL on failure
  1409. *
  1410. * This function request the current scan results from the driver and updates
  1411. * the local BSS list wpa_s->bss. The caller is responsible for freeing the
  1412. * results with wpa_scan_results_free().
  1413. */
  1414. struct wpa_scan_results *
  1415. wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
  1416. struct scan_info *info, int new_scan)
  1417. {
  1418. struct wpa_scan_results *scan_res;
  1419. size_t i;
  1420. int (*compar)(const void *, const void *) = wpa_scan_result_compar;
  1421. scan_res = wpa_drv_get_scan_results2(wpa_s);
  1422. if (scan_res == NULL) {
  1423. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
  1424. return NULL;
  1425. }
  1426. if (scan_res->fetch_time.sec == 0) {
  1427. /*
  1428. * Make sure we have a valid timestamp if the driver wrapper
  1429. * does not set this.
  1430. */
  1431. os_get_reltime(&scan_res->fetch_time);
  1432. }
  1433. filter_scan_res(wpa_s, scan_res);
  1434. #ifdef CONFIG_WPS
  1435. if (wpas_wps_searching(wpa_s)) {
  1436. wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
  1437. "provisioning rules");
  1438. compar = wpa_scan_result_wps_compar;
  1439. }
  1440. #endif /* CONFIG_WPS */
  1441. qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
  1442. compar);
  1443. dump_scan_res(scan_res);
  1444. wpa_bss_update_start(wpa_s);
  1445. for (i = 0; i < scan_res->num; i++)
  1446. wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
  1447. &scan_res->fetch_time);
  1448. wpa_bss_update_end(wpa_s, info, new_scan);
  1449. return scan_res;
  1450. }
  1451. /**
  1452. * wpa_supplicant_update_scan_results - Update scan results from the driver
  1453. * @wpa_s: Pointer to wpa_supplicant data
  1454. * Returns: 0 on success, -1 on failure
  1455. *
  1456. * This function updates the BSS table within wpa_supplicant based on the
  1457. * currently available scan results from the driver without requesting a new
  1458. * scan. This is used in cases where the driver indicates an association
  1459. * (including roaming within ESS) and wpa_supplicant does not yet have the
  1460. * needed information to complete the connection (e.g., to perform validation
  1461. * steps in 4-way handshake).
  1462. */
  1463. int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
  1464. {
  1465. struct wpa_scan_results *scan_res;
  1466. scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
  1467. if (scan_res == NULL)
  1468. return -1;
  1469. wpa_scan_results_free(scan_res);
  1470. return 0;
  1471. }
  1472. /**
  1473. * scan_only_handler - Reports scan results
  1474. */
  1475. void scan_only_handler(struct wpa_supplicant *wpa_s,
  1476. struct wpa_scan_results *scan_res)
  1477. {
  1478. wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
  1479. if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
  1480. wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
  1481. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
  1482. wpa_s->manual_scan_id);
  1483. wpa_s->manual_scan_use_id = 0;
  1484. } else {
  1485. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
  1486. }
  1487. wpas_notify_scan_results(wpa_s);
  1488. wpas_notify_scan_done(wpa_s, 1);
  1489. if (wpa_s->scan_work) {
  1490. struct wpa_radio_work *work = wpa_s->scan_work;
  1491. wpa_s->scan_work = NULL;
  1492. radio_work_done(work);
  1493. }
  1494. }
  1495. int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
  1496. {
  1497. return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
  1498. }
  1499. struct wpa_driver_scan_params *
  1500. wpa_scan_clone_params(const struct wpa_driver_scan_params *src)
  1501. {
  1502. struct wpa_driver_scan_params *params;
  1503. size_t i;
  1504. u8 *n;
  1505. params = os_zalloc(sizeof(*params));
  1506. if (params == NULL)
  1507. return NULL;
  1508. for (i = 0; i < src->num_ssids; i++) {
  1509. if (src->ssids[i].ssid) {
  1510. n = os_malloc(src->ssids[i].ssid_len);
  1511. if (n == NULL)
  1512. goto failed;
  1513. os_memcpy(n, src->ssids[i].ssid,
  1514. src->ssids[i].ssid_len);
  1515. params->ssids[i].ssid = n;
  1516. params->ssids[i].ssid_len = src->ssids[i].ssid_len;
  1517. }
  1518. }
  1519. params->num_ssids = src->num_ssids;
  1520. if (src->extra_ies) {
  1521. n = os_malloc(src->extra_ies_len);
  1522. if (n == NULL)
  1523. goto failed;
  1524. os_memcpy(n, src->extra_ies, src->extra_ies_len);
  1525. params->extra_ies = n;
  1526. params->extra_ies_len = src->extra_ies_len;
  1527. }
  1528. if (src->freqs) {
  1529. int len = int_array_len(src->freqs);
  1530. params->freqs = os_malloc((len + 1) * sizeof(int));
  1531. if (params->freqs == NULL)
  1532. goto failed;
  1533. os_memcpy(params->freqs, src->freqs, (len + 1) * sizeof(int));
  1534. }
  1535. if (src->filter_ssids) {
  1536. params->filter_ssids = os_malloc(sizeof(params->filter_ssids) *
  1537. src->num_filter_ssids);
  1538. if (params->filter_ssids == NULL)
  1539. goto failed;
  1540. os_memcpy(params->filter_ssids, src->filter_ssids,
  1541. sizeof(params->filter_ssids) * src->num_filter_ssids);
  1542. params->num_filter_ssids = src->num_filter_ssids;
  1543. }
  1544. params->filter_rssi = src->filter_rssi;
  1545. params->p2p_probe = src->p2p_probe;
  1546. params->only_new_results = src->only_new_results;
  1547. return params;
  1548. failed:
  1549. wpa_scan_free_params(params);
  1550. return NULL;
  1551. }
  1552. void wpa_scan_free_params(struct wpa_driver_scan_params *params)
  1553. {
  1554. size_t i;
  1555. if (params == NULL)
  1556. return;
  1557. for (i = 0; i < params->num_ssids; i++)
  1558. os_free((u8 *) params->ssids[i].ssid);
  1559. os_free((u8 *) params->extra_ies);
  1560. os_free(params->freqs);
  1561. os_free(params->filter_ssids);
  1562. os_free(params);
  1563. }