scan.c 48 KB

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