scan.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803
  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_s->external_scan_running) {
  454. struct os_reltime now, diff;
  455. os_get_reltime(&now);
  456. os_reltime_sub(&now, &wpa_s->scan_start_time, &diff);
  457. if (diff.sec < 30) {
  458. wpa_dbg(wpa_s, MSG_DEBUG, "Externally triggered scan running - Reschedule the incoming scan req");
  459. wpa_supplicant_req_scan(wpa_s, 1, 0);
  460. return;
  461. }
  462. }
  463. if (!wpa_supplicant_enabled_networks(wpa_s) &&
  464. wpa_s->scan_req == NORMAL_SCAN_REQ) {
  465. wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
  466. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  467. return;
  468. }
  469. if (wpa_s->conf->ap_scan != 0 &&
  470. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
  471. wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
  472. "overriding ap_scan configuration");
  473. wpa_s->conf->ap_scan = 0;
  474. wpas_notify_ap_scan_changed(wpa_s);
  475. }
  476. if (wpa_s->conf->ap_scan == 0) {
  477. wpa_supplicant_gen_assoc_event(wpa_s);
  478. return;
  479. }
  480. #ifdef CONFIG_P2P
  481. if (wpas_p2p_in_progress(wpa_s) || wpas_wpa_is_in_progress(wpa_s, 0)) {
  482. wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan while P2P operation is in progress");
  483. wpa_supplicant_req_scan(wpa_s, 5, 0);
  484. return;
  485. }
  486. #endif /* CONFIG_P2P */
  487. if (wpa_s->conf->ap_scan == 2)
  488. max_ssids = 1;
  489. else {
  490. max_ssids = wpa_s->max_scan_ssids;
  491. if (max_ssids > WPAS_MAX_SCAN_SSIDS)
  492. max_ssids = WPAS_MAX_SCAN_SSIDS;
  493. }
  494. wpa_s->last_scan_req = wpa_s->scan_req;
  495. wpa_s->scan_req = NORMAL_SCAN_REQ;
  496. os_memset(&params, 0, sizeof(params));
  497. prev_state = wpa_s->wpa_state;
  498. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  499. wpa_s->wpa_state == WPA_INACTIVE)
  500. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  501. /*
  502. * If autoscan has set its own scanning parameters
  503. */
  504. if (wpa_s->autoscan_params != NULL) {
  505. scan_params = wpa_s->autoscan_params;
  506. goto scan;
  507. }
  508. if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
  509. wpa_s->connect_without_scan) {
  510. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  511. if (ssid == wpa_s->connect_without_scan)
  512. break;
  513. }
  514. wpa_s->connect_without_scan = NULL;
  515. if (ssid) {
  516. wpa_printf(MSG_DEBUG, "Start a pre-selected network "
  517. "without scan step");
  518. wpa_supplicant_associate(wpa_s, NULL, ssid);
  519. return;
  520. }
  521. }
  522. #ifdef CONFIG_P2P
  523. if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
  524. wpa_s->go_params) {
  525. wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
  526. wpa_s->p2p_in_provisioning,
  527. wpa_s->show_group_started);
  528. params.ssids[0].ssid = wpa_s->go_params->ssid;
  529. params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
  530. params.num_ssids = 1;
  531. goto ssid_list_set;
  532. }
  533. #endif /* CONFIG_P2P */
  534. /* Find the starting point from which to continue scanning */
  535. ssid = wpa_s->conf->ssid;
  536. if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
  537. while (ssid) {
  538. if (ssid == wpa_s->prev_scan_ssid) {
  539. ssid = ssid->next;
  540. break;
  541. }
  542. ssid = ssid->next;
  543. }
  544. }
  545. if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
  546. wpa_s->conf->ap_scan == 2) {
  547. wpa_s->connect_without_scan = NULL;
  548. wpa_s->prev_scan_wildcard = 0;
  549. wpa_supplicant_assoc_try(wpa_s, ssid);
  550. return;
  551. } else if (wpa_s->conf->ap_scan == 2) {
  552. /*
  553. * User-initiated scan request in ap_scan == 2; scan with
  554. * wildcard SSID.
  555. */
  556. ssid = NULL;
  557. } else {
  558. struct wpa_ssid *start = ssid, *tssid;
  559. int freqs_set = 0;
  560. if (ssid == NULL && max_ssids > 1)
  561. ssid = wpa_s->conf->ssid;
  562. while (ssid) {
  563. if (!wpas_network_disabled(wpa_s, ssid) &&
  564. ssid->scan_ssid) {
  565. wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
  566. ssid->ssid, ssid->ssid_len);
  567. params.ssids[params.num_ssids].ssid =
  568. ssid->ssid;
  569. params.ssids[params.num_ssids].ssid_len =
  570. ssid->ssid_len;
  571. params.num_ssids++;
  572. if (params.num_ssids + 1 >= max_ssids)
  573. break;
  574. }
  575. ssid = ssid->next;
  576. if (ssid == start)
  577. break;
  578. if (ssid == NULL && max_ssids > 1 &&
  579. start != wpa_s->conf->ssid)
  580. ssid = wpa_s->conf->ssid;
  581. }
  582. for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
  583. if (wpas_network_disabled(wpa_s, tssid))
  584. continue;
  585. if ((params.freqs || !freqs_set) && tssid->scan_freq) {
  586. int_array_concat(&params.freqs,
  587. tssid->scan_freq);
  588. } else {
  589. os_free(params.freqs);
  590. params.freqs = NULL;
  591. }
  592. freqs_set = 1;
  593. }
  594. int_array_sort_unique(params.freqs);
  595. }
  596. if (ssid && max_ssids == 1) {
  597. /*
  598. * If the driver is limited to 1 SSID at a time interleave
  599. * wildcard SSID scans with specific SSID scans to avoid
  600. * waiting a long time for a wildcard scan.
  601. */
  602. if (!wpa_s->prev_scan_wildcard) {
  603. params.ssids[0].ssid = NULL;
  604. params.ssids[0].ssid_len = 0;
  605. wpa_s->prev_scan_wildcard = 1;
  606. wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
  607. "wildcard SSID (Interleave with specific)");
  608. } else {
  609. wpa_s->prev_scan_ssid = ssid;
  610. wpa_s->prev_scan_wildcard = 0;
  611. wpa_dbg(wpa_s, MSG_DEBUG,
  612. "Starting AP scan for specific SSID: %s",
  613. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  614. }
  615. } else if (ssid) {
  616. /* max_ssids > 1 */
  617. wpa_s->prev_scan_ssid = ssid;
  618. wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
  619. "the scan request");
  620. params.num_ssids++;
  621. } else if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
  622. wpa_s->manual_scan_passive && params.num_ssids == 0) {
  623. wpa_dbg(wpa_s, MSG_DEBUG, "Use passive scan based on manual request");
  624. } else {
  625. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  626. params.num_ssids++;
  627. wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
  628. "SSID");
  629. }
  630. #ifdef CONFIG_P2P
  631. ssid_list_set:
  632. #endif /* CONFIG_P2P */
  633. wpa_supplicant_optimize_freqs(wpa_s, &params);
  634. extra_ie = wpa_supplicant_extra_ies(wpa_s);
  635. if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
  636. wpa_s->manual_scan_only_new)
  637. params.only_new_results = 1;
  638. if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
  639. wpa_s->manual_scan_freqs) {
  640. wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
  641. params.freqs = wpa_s->manual_scan_freqs;
  642. wpa_s->manual_scan_freqs = NULL;
  643. }
  644. if (params.freqs == NULL && wpa_s->next_scan_freqs) {
  645. wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
  646. "generated frequency list");
  647. params.freqs = wpa_s->next_scan_freqs;
  648. } else
  649. os_free(wpa_s->next_scan_freqs);
  650. wpa_s->next_scan_freqs = NULL;
  651. wpa_setband_scan_freqs(wpa_s, &params);
  652. /* See if user specified frequencies. If so, scan only those. */
  653. if (wpa_s->conf->freq_list && !params.freqs) {
  654. wpa_dbg(wpa_s, MSG_DEBUG,
  655. "Optimize scan based on conf->freq_list");
  656. int_array_concat(&params.freqs, wpa_s->conf->freq_list);
  657. }
  658. /* Use current associated channel? */
  659. if (wpa_s->conf->scan_cur_freq && !params.freqs) {
  660. unsigned int num = wpa_s->num_multichan_concurrent;
  661. params.freqs = os_calloc(num + 1, sizeof(int));
  662. if (params.freqs) {
  663. num = get_shared_radio_freqs(wpa_s, params.freqs, num);
  664. if (num > 0) {
  665. wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
  666. "current operating channels since "
  667. "scan_cur_freq is enabled");
  668. } else {
  669. os_free(params.freqs);
  670. params.freqs = NULL;
  671. }
  672. }
  673. }
  674. params.filter_ssids = wpa_supplicant_build_filter_ssids(
  675. wpa_s->conf, &params.num_filter_ssids);
  676. if (extra_ie) {
  677. params.extra_ies = wpabuf_head(extra_ie);
  678. params.extra_ies_len = wpabuf_len(extra_ie);
  679. }
  680. #ifdef CONFIG_P2P
  681. if (wpa_s->p2p_in_provisioning ||
  682. (wpa_s->show_group_started && wpa_s->go_params)) {
  683. /*
  684. * The interface may not yet be in P2P mode, so we have to
  685. * explicitly request P2P probe to disable CCK rates.
  686. */
  687. params.p2p_probe = 1;
  688. }
  689. #endif /* CONFIG_P2P */
  690. scan_params = &params;
  691. scan:
  692. #ifdef CONFIG_P2P
  693. /*
  694. * If the driver does not support multi-channel concurrency and a
  695. * virtual interface that shares the same radio with the wpa_s interface
  696. * is operating there may not be need to scan other channels apart from
  697. * the current operating channel on the other virtual interface. Filter
  698. * out other channels in case we are trying to find a connection for a
  699. * station interface when we are not configured to prefer station
  700. * connection and a concurrent operation is already in process.
  701. */
  702. if (wpa_s->scan_for_connection &&
  703. wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
  704. !scan_params->freqs && !params.freqs &&
  705. wpas_is_p2p_prioritized(wpa_s) &&
  706. wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
  707. non_p2p_network_enabled(wpa_s)) {
  708. unsigned int num = wpa_s->num_multichan_concurrent;
  709. params.freqs = os_calloc(num + 1, sizeof(int));
  710. if (params.freqs) {
  711. num = get_shared_radio_freqs(wpa_s, params.freqs, num);
  712. if (num > 0 && num == wpa_s->num_multichan_concurrent) {
  713. wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
  714. } else {
  715. os_free(params.freqs);
  716. params.freqs = NULL;
  717. }
  718. }
  719. }
  720. #endif /* CONFIG_P2P */
  721. ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
  722. if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
  723. !wpa_s->manual_scan_freqs) {
  724. /* Restore manual_scan_freqs for the next attempt */
  725. wpa_s->manual_scan_freqs = params.freqs;
  726. params.freqs = NULL;
  727. }
  728. wpabuf_free(extra_ie);
  729. os_free(params.freqs);
  730. os_free(params.filter_ssids);
  731. if (ret) {
  732. wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
  733. if (prev_state != wpa_s->wpa_state)
  734. wpa_supplicant_set_state(wpa_s, prev_state);
  735. /* Restore scan_req since we will try to scan again */
  736. wpa_s->scan_req = wpa_s->last_scan_req;
  737. wpa_supplicant_req_scan(wpa_s, 1, 0);
  738. } else {
  739. wpa_s->scan_for_connection = 0;
  740. }
  741. }
  742. void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
  743. {
  744. struct os_reltime remaining, new_int;
  745. int cancelled;
  746. cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
  747. &remaining);
  748. new_int.sec = sec;
  749. new_int.usec = 0;
  750. if (cancelled && os_reltime_before(&remaining, &new_int)) {
  751. new_int.sec = remaining.sec;
  752. new_int.usec = remaining.usec;
  753. }
  754. if (cancelled) {
  755. eloop_register_timeout(new_int.sec, new_int.usec,
  756. wpa_supplicant_scan, wpa_s, NULL);
  757. }
  758. wpa_s->scan_interval = sec;
  759. }
  760. /**
  761. * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
  762. * @wpa_s: Pointer to wpa_supplicant data
  763. * @sec: Number of seconds after which to scan
  764. * @usec: Number of microseconds after which to scan
  765. *
  766. * This function is used to schedule a scan for neighboring access points after
  767. * the specified time.
  768. */
  769. void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
  770. {
  771. if (eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL))
  772. {
  773. wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d sec %d usec",
  774. sec, usec);
  775. return;
  776. }
  777. wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
  778. sec, usec);
  779. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  780. eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
  781. }
  782. /**
  783. * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
  784. * @wpa_s: Pointer to wpa_supplicant data
  785. * @sec: Number of seconds after which to scan
  786. * @usec: Number of microseconds after which to scan
  787. * Returns: 0 on success or -1 otherwise
  788. *
  789. * This function is used to schedule periodic scans for neighboring
  790. * access points after the specified time.
  791. */
  792. int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
  793. int sec, int usec)
  794. {
  795. if (!wpa_s->sched_scan_supported)
  796. return -1;
  797. eloop_register_timeout(sec, usec,
  798. wpa_supplicant_delayed_sched_scan_timeout,
  799. wpa_s, NULL);
  800. return 0;
  801. }
  802. /**
  803. * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
  804. * @wpa_s: Pointer to wpa_supplicant data
  805. * Returns: 0 is sched_scan was started or -1 otherwise
  806. *
  807. * This function is used to schedule periodic scans for neighboring
  808. * access points repeating the scan continuously.
  809. */
  810. int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
  811. {
  812. struct wpa_driver_scan_params params;
  813. struct wpa_driver_scan_params *scan_params;
  814. enum wpa_states prev_state;
  815. struct wpa_ssid *ssid = NULL;
  816. struct wpabuf *extra_ie = NULL;
  817. int ret;
  818. unsigned int max_sched_scan_ssids;
  819. int wildcard = 0;
  820. int need_ssids;
  821. if (!wpa_s->sched_scan_supported)
  822. return -1;
  823. if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
  824. max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
  825. else
  826. max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
  827. if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
  828. return -1;
  829. if (wpa_s->sched_scanning) {
  830. wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
  831. return 0;
  832. }
  833. need_ssids = 0;
  834. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  835. if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
  836. /* Use wildcard SSID to find this network */
  837. wildcard = 1;
  838. } else if (!wpas_network_disabled(wpa_s, ssid) &&
  839. ssid->ssid_len)
  840. need_ssids++;
  841. #ifdef CONFIG_WPS
  842. if (!wpas_network_disabled(wpa_s, ssid) &&
  843. ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
  844. /*
  845. * Normal scan is more reliable and faster for WPS
  846. * operations and since these are for short periods of
  847. * time, the benefit of trying to use sched_scan would
  848. * be limited.
  849. */
  850. wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
  851. "sched_scan for WPS");
  852. return -1;
  853. }
  854. #endif /* CONFIG_WPS */
  855. }
  856. if (wildcard)
  857. need_ssids++;
  858. if (wpa_s->normal_scans < 3 &&
  859. (need_ssids <= wpa_s->max_scan_ssids ||
  860. wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
  861. /*
  862. * When normal scan can speed up operations, use that for the
  863. * first operations before starting the sched_scan to allow
  864. * user space sleep more. We do this only if the normal scan
  865. * has functionality that is suitable for this or if the
  866. * sched_scan does not have better support for multiple SSIDs.
  867. */
  868. wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
  869. "sched_scan for initial scans (normal_scans=%d)",
  870. wpa_s->normal_scans);
  871. return -1;
  872. }
  873. os_memset(&params, 0, sizeof(params));
  874. /* If we can't allocate space for the filters, we just don't filter */
  875. params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
  876. sizeof(struct wpa_driver_scan_filter));
  877. prev_state = wpa_s->wpa_state;
  878. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  879. wpa_s->wpa_state == WPA_INACTIVE)
  880. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  881. if (wpa_s->autoscan_params != NULL) {
  882. scan_params = wpa_s->autoscan_params;
  883. goto scan;
  884. }
  885. /* Find the starting point from which to continue scanning */
  886. ssid = wpa_s->conf->ssid;
  887. if (wpa_s->prev_sched_ssid) {
  888. while (ssid) {
  889. if (ssid == wpa_s->prev_sched_ssid) {
  890. ssid = ssid->next;
  891. break;
  892. }
  893. ssid = ssid->next;
  894. }
  895. }
  896. if (!ssid || !wpa_s->prev_sched_ssid) {
  897. wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
  898. if (wpa_s->conf->sched_scan_interval)
  899. wpa_s->sched_scan_interval =
  900. wpa_s->conf->sched_scan_interval;
  901. if (wpa_s->sched_scan_interval == 0)
  902. wpa_s->sched_scan_interval = 10;
  903. wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
  904. wpa_s->first_sched_scan = 1;
  905. ssid = wpa_s->conf->ssid;
  906. wpa_s->prev_sched_ssid = ssid;
  907. }
  908. if (wildcard) {
  909. wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
  910. params.num_ssids++;
  911. }
  912. while (ssid) {
  913. if (wpas_network_disabled(wpa_s, ssid))
  914. goto next;
  915. if (params.num_filter_ssids < wpa_s->max_match_sets &&
  916. params.filter_ssids && ssid->ssid && ssid->ssid_len) {
  917. wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
  918. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  919. os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
  920. ssid->ssid, ssid->ssid_len);
  921. params.filter_ssids[params.num_filter_ssids].ssid_len =
  922. ssid->ssid_len;
  923. params.num_filter_ssids++;
  924. } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
  925. {
  926. wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
  927. "filter for sched_scan - drop filter");
  928. os_free(params.filter_ssids);
  929. params.filter_ssids = NULL;
  930. params.num_filter_ssids = 0;
  931. }
  932. if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
  933. if (params.num_ssids == max_sched_scan_ssids)
  934. break; /* only room for broadcast SSID */
  935. wpa_dbg(wpa_s, MSG_DEBUG,
  936. "add to active scan ssid: %s",
  937. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  938. params.ssids[params.num_ssids].ssid =
  939. ssid->ssid;
  940. params.ssids[params.num_ssids].ssid_len =
  941. ssid->ssid_len;
  942. params.num_ssids++;
  943. if (params.num_ssids >= max_sched_scan_ssids) {
  944. wpa_s->prev_sched_ssid = ssid;
  945. do {
  946. ssid = ssid->next;
  947. } while (ssid &&
  948. (wpas_network_disabled(wpa_s, ssid) ||
  949. !ssid->scan_ssid));
  950. break;
  951. }
  952. }
  953. next:
  954. wpa_s->prev_sched_ssid = ssid;
  955. ssid = ssid->next;
  956. }
  957. if (params.num_filter_ssids == 0) {
  958. os_free(params.filter_ssids);
  959. params.filter_ssids = NULL;
  960. }
  961. extra_ie = wpa_supplicant_extra_ies(wpa_s);
  962. if (extra_ie) {
  963. params.extra_ies = wpabuf_head(extra_ie);
  964. params.extra_ies_len = wpabuf_len(extra_ie);
  965. }
  966. scan_params = &params;
  967. scan:
  968. if (ssid || !wpa_s->first_sched_scan) {
  969. wpa_dbg(wpa_s, MSG_DEBUG,
  970. "Starting sched scan: interval %d timeout %d",
  971. wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
  972. } else {
  973. wpa_dbg(wpa_s, MSG_DEBUG,
  974. "Starting sched scan: interval %d (no timeout)",
  975. wpa_s->sched_scan_interval);
  976. }
  977. wpa_setband_scan_freqs(wpa_s, scan_params);
  978. ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params,
  979. wpa_s->sched_scan_interval);
  980. wpabuf_free(extra_ie);
  981. os_free(params.filter_ssids);
  982. if (ret) {
  983. wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
  984. if (prev_state != wpa_s->wpa_state)
  985. wpa_supplicant_set_state(wpa_s, prev_state);
  986. return ret;
  987. }
  988. /* If we have more SSIDs to scan, add a timeout so we scan them too */
  989. if (ssid || !wpa_s->first_sched_scan) {
  990. wpa_s->sched_scan_timed_out = 0;
  991. eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
  992. wpa_supplicant_sched_scan_timeout,
  993. wpa_s, NULL);
  994. wpa_s->first_sched_scan = 0;
  995. wpa_s->sched_scan_timeout /= 2;
  996. wpa_s->sched_scan_interval *= 2;
  997. if (wpa_s->sched_scan_timeout < wpa_s->sched_scan_interval) {
  998. wpa_s->sched_scan_interval = 10;
  999. wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
  1000. }
  1001. }
  1002. /* If there is no more ssids, start next time from the beginning */
  1003. if (!ssid)
  1004. wpa_s->prev_sched_ssid = NULL;
  1005. return 0;
  1006. }
  1007. /**
  1008. * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
  1009. * @wpa_s: Pointer to wpa_supplicant data
  1010. *
  1011. * This function is used to cancel a scan request scheduled with
  1012. * wpa_supplicant_req_scan().
  1013. */
  1014. void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
  1015. {
  1016. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
  1017. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  1018. }
  1019. /**
  1020. * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
  1021. * @wpa_s: Pointer to wpa_supplicant data
  1022. *
  1023. * This function is used to stop a delayed scheduled scan.
  1024. */
  1025. void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
  1026. {
  1027. if (!wpa_s->sched_scan_supported)
  1028. return;
  1029. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
  1030. eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
  1031. wpa_s, NULL);
  1032. }
  1033. /**
  1034. * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
  1035. * @wpa_s: Pointer to wpa_supplicant data
  1036. *
  1037. * This function is used to stop a periodic scheduled scan.
  1038. */
  1039. void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
  1040. {
  1041. if (!wpa_s->sched_scanning)
  1042. return;
  1043. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
  1044. eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
  1045. wpa_supplicant_stop_sched_scan(wpa_s);
  1046. }
  1047. /**
  1048. * wpa_supplicant_notify_scanning - Indicate possible scan state change
  1049. * @wpa_s: Pointer to wpa_supplicant data
  1050. * @scanning: Whether scanning is currently in progress
  1051. *
  1052. * This function is to generate scanning notifycations. It is called whenever
  1053. * there may have been a change in scanning (scan started, completed, stopped).
  1054. * wpas_notify_scanning() is called whenever the scanning state changed from the
  1055. * previously notified state.
  1056. */
  1057. void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
  1058. int scanning)
  1059. {
  1060. if (wpa_s->scanning != scanning) {
  1061. wpa_s->scanning = scanning;
  1062. wpas_notify_scanning(wpa_s);
  1063. }
  1064. }
  1065. static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
  1066. {
  1067. int rate = 0;
  1068. const u8 *ie;
  1069. int i;
  1070. ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
  1071. for (i = 0; ie && i < ie[1]; i++) {
  1072. if ((ie[i + 2] & 0x7f) > rate)
  1073. rate = ie[i + 2] & 0x7f;
  1074. }
  1075. ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
  1076. for (i = 0; ie && i < ie[1]; i++) {
  1077. if ((ie[i + 2] & 0x7f) > rate)
  1078. rate = ie[i + 2] & 0x7f;
  1079. }
  1080. return rate;
  1081. }
  1082. /**
  1083. * wpa_scan_get_ie - Fetch a specified information element from a scan result
  1084. * @res: Scan result entry
  1085. * @ie: Information element identitifier (WLAN_EID_*)
  1086. * Returns: Pointer to the information element (id field) or %NULL if not found
  1087. *
  1088. * This function returns the first matching information element in the scan
  1089. * result.
  1090. */
  1091. const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
  1092. {
  1093. const u8 *end, *pos;
  1094. pos = (const u8 *) (res + 1);
  1095. end = pos + res->ie_len;
  1096. while (pos + 1 < end) {
  1097. if (pos + 2 + pos[1] > end)
  1098. break;
  1099. if (pos[0] == ie)
  1100. return pos;
  1101. pos += 2 + pos[1];
  1102. }
  1103. return NULL;
  1104. }
  1105. /**
  1106. * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
  1107. * @res: Scan result entry
  1108. * @vendor_type: Vendor type (four octets starting the IE payload)
  1109. * Returns: Pointer to the information element (id field) or %NULL if not found
  1110. *
  1111. * This function returns the first matching information element in the scan
  1112. * result.
  1113. */
  1114. const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
  1115. u32 vendor_type)
  1116. {
  1117. const u8 *end, *pos;
  1118. pos = (const u8 *) (res + 1);
  1119. end = pos + res->ie_len;
  1120. while (pos + 1 < end) {
  1121. if (pos + 2 + pos[1] > end)
  1122. break;
  1123. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  1124. vendor_type == WPA_GET_BE32(&pos[2]))
  1125. return pos;
  1126. pos += 2 + pos[1];
  1127. }
  1128. return NULL;
  1129. }
  1130. /**
  1131. * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
  1132. * @res: Scan result entry
  1133. * @vendor_type: Vendor type (four octets starting the IE payload)
  1134. * Returns: Pointer to the information element (id field) or %NULL if not found
  1135. *
  1136. * This function returns the first matching information element in the scan
  1137. * result.
  1138. *
  1139. * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
  1140. * from Beacon frames instead of either Beacon or Probe Response frames.
  1141. */
  1142. const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
  1143. u32 vendor_type)
  1144. {
  1145. const u8 *end, *pos;
  1146. if (res->beacon_ie_len == 0)
  1147. return NULL;
  1148. pos = (const u8 *) (res + 1);
  1149. pos += res->ie_len;
  1150. end = pos + res->beacon_ie_len;
  1151. while (pos + 1 < end) {
  1152. if (pos + 2 + pos[1] > end)
  1153. break;
  1154. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  1155. vendor_type == WPA_GET_BE32(&pos[2]))
  1156. return pos;
  1157. pos += 2 + pos[1];
  1158. }
  1159. return NULL;
  1160. }
  1161. /**
  1162. * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
  1163. * @res: Scan result entry
  1164. * @vendor_type: Vendor type (four octets starting the IE payload)
  1165. * Returns: Pointer to the information element payload or %NULL if not found
  1166. *
  1167. * This function returns concatenated payload of possibly fragmented vendor
  1168. * specific information elements in the scan result. The caller is responsible
  1169. * for freeing the returned buffer.
  1170. */
  1171. struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
  1172. u32 vendor_type)
  1173. {
  1174. struct wpabuf *buf;
  1175. const u8 *end, *pos;
  1176. buf = wpabuf_alloc(res->ie_len);
  1177. if (buf == NULL)
  1178. return NULL;
  1179. pos = (const u8 *) (res + 1);
  1180. end = pos + res->ie_len;
  1181. while (pos + 1 < end) {
  1182. if (pos + 2 + pos[1] > end)
  1183. break;
  1184. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  1185. vendor_type == WPA_GET_BE32(&pos[2]))
  1186. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  1187. pos += 2 + pos[1];
  1188. }
  1189. if (wpabuf_len(buf) == 0) {
  1190. wpabuf_free(buf);
  1191. buf = NULL;
  1192. }
  1193. return buf;
  1194. }
  1195. /*
  1196. * Channels with a great SNR can operate at full rate. What is a great SNR?
  1197. * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
  1198. * rule of thumb is that any SNR above 20 is good." This one
  1199. * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
  1200. * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
  1201. * conservative value.
  1202. */
  1203. #define GREAT_SNR 30
  1204. /* Compare function for sorting scan results. Return >0 if @b is considered
  1205. * better. */
  1206. static int wpa_scan_result_compar(const void *a, const void *b)
  1207. {
  1208. #define IS_5GHZ(n) (n > 4000)
  1209. #define MIN(a,b) a < b ? a : b
  1210. struct wpa_scan_res **_wa = (void *) a;
  1211. struct wpa_scan_res **_wb = (void *) b;
  1212. struct wpa_scan_res *wa = *_wa;
  1213. struct wpa_scan_res *wb = *_wb;
  1214. int wpa_a, wpa_b, maxrate_a, maxrate_b;
  1215. int snr_a, snr_b;
  1216. /* WPA/WPA2 support preferred */
  1217. wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
  1218. wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
  1219. wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
  1220. wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
  1221. if (wpa_b && !wpa_a)
  1222. return 1;
  1223. if (!wpa_b && wpa_a)
  1224. return -1;
  1225. /* privacy support preferred */
  1226. if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
  1227. (wb->caps & IEEE80211_CAP_PRIVACY))
  1228. return 1;
  1229. if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
  1230. (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
  1231. return -1;
  1232. if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
  1233. !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
  1234. snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
  1235. snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
  1236. } else {
  1237. /* Not suitable information to calculate SNR, so use level */
  1238. snr_a = wa->level;
  1239. snr_b = wb->level;
  1240. }
  1241. /* best/max rate preferred if SNR close enough */
  1242. if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
  1243. (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
  1244. maxrate_a = wpa_scan_get_max_rate(wa);
  1245. maxrate_b = wpa_scan_get_max_rate(wb);
  1246. if (maxrate_a != maxrate_b)
  1247. return maxrate_b - maxrate_a;
  1248. if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
  1249. return IS_5GHZ(wa->freq) ? -1 : 1;
  1250. }
  1251. /* use freq for channel preference */
  1252. /* all things being equal, use SNR; if SNRs are
  1253. * identical, use quality values since some drivers may only report
  1254. * that value and leave the signal level zero */
  1255. if (snr_b == snr_a)
  1256. return wb->qual - wa->qual;
  1257. return snr_b - snr_a;
  1258. #undef MIN
  1259. #undef IS_5GHZ
  1260. }
  1261. #ifdef CONFIG_WPS
  1262. /* Compare function for sorting scan results when searching a WPS AP for
  1263. * provisioning. Return >0 if @b is considered better. */
  1264. static int wpa_scan_result_wps_compar(const void *a, const void *b)
  1265. {
  1266. struct wpa_scan_res **_wa = (void *) a;
  1267. struct wpa_scan_res **_wb = (void *) b;
  1268. struct wpa_scan_res *wa = *_wa;
  1269. struct wpa_scan_res *wb = *_wb;
  1270. int uses_wps_a, uses_wps_b;
  1271. struct wpabuf *wps_a, *wps_b;
  1272. int res;
  1273. /* Optimization - check WPS IE existence before allocated memory and
  1274. * doing full reassembly. */
  1275. uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
  1276. uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
  1277. if (uses_wps_a && !uses_wps_b)
  1278. return -1;
  1279. if (!uses_wps_a && uses_wps_b)
  1280. return 1;
  1281. if (uses_wps_a && uses_wps_b) {
  1282. wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
  1283. wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
  1284. res = wps_ap_priority_compar(wps_a, wps_b);
  1285. wpabuf_free(wps_a);
  1286. wpabuf_free(wps_b);
  1287. if (res)
  1288. return res;
  1289. }
  1290. /*
  1291. * Do not use current AP security policy as a sorting criteria during
  1292. * WPS provisioning step since the AP may get reconfigured at the
  1293. * completion of provisioning.
  1294. */
  1295. /* all things being equal, use signal level; if signal levels are
  1296. * identical, use quality values since some drivers may only report
  1297. * that value and leave the signal level zero */
  1298. if (wb->level == wa->level)
  1299. return wb->qual - wa->qual;
  1300. return wb->level - wa->level;
  1301. }
  1302. #endif /* CONFIG_WPS */
  1303. static void dump_scan_res(struct wpa_scan_results *scan_res)
  1304. {
  1305. #ifndef CONFIG_NO_STDOUT_DEBUG
  1306. size_t i;
  1307. if (scan_res->res == NULL || scan_res->num == 0)
  1308. return;
  1309. wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
  1310. for (i = 0; i < scan_res->num; i++) {
  1311. struct wpa_scan_res *r = scan_res->res[i];
  1312. u8 *pos;
  1313. if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
  1314. == WPA_SCAN_LEVEL_DBM) {
  1315. int snr = r->level - r->noise;
  1316. wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
  1317. "noise=%d level=%d snr=%d%s flags=0x%x "
  1318. "age=%u",
  1319. MAC2STR(r->bssid), r->freq, r->qual,
  1320. r->noise, r->level, snr,
  1321. snr >= GREAT_SNR ? "*" : "", r->flags,
  1322. r->age);
  1323. } else {
  1324. wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
  1325. "noise=%d level=%d flags=0x%x age=%u",
  1326. MAC2STR(r->bssid), r->freq, r->qual,
  1327. r->noise, r->level, r->flags, r->age);
  1328. }
  1329. pos = (u8 *) (r + 1);
  1330. if (r->ie_len)
  1331. wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
  1332. pos += r->ie_len;
  1333. if (r->beacon_ie_len)
  1334. wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
  1335. pos, r->beacon_ie_len);
  1336. }
  1337. #endif /* CONFIG_NO_STDOUT_DEBUG */
  1338. }
  1339. /**
  1340. * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
  1341. * @wpa_s: Pointer to wpa_supplicant data
  1342. * @bssid: BSSID to check
  1343. * Returns: 0 if the BSSID is filtered or 1 if not
  1344. *
  1345. * This function is used to filter out specific BSSIDs from scan reslts mainly
  1346. * for testing purposes (SET bssid_filter ctrl_iface command).
  1347. */
  1348. int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
  1349. const u8 *bssid)
  1350. {
  1351. size_t i;
  1352. if (wpa_s->bssid_filter == NULL)
  1353. return 1;
  1354. for (i = 0; i < wpa_s->bssid_filter_count; i++) {
  1355. if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
  1356. ETH_ALEN) == 0)
  1357. return 1;
  1358. }
  1359. return 0;
  1360. }
  1361. static void filter_scan_res(struct wpa_supplicant *wpa_s,
  1362. struct wpa_scan_results *res)
  1363. {
  1364. size_t i, j;
  1365. if (wpa_s->bssid_filter == NULL)
  1366. return;
  1367. for (i = 0, j = 0; i < res->num; i++) {
  1368. if (wpa_supplicant_filter_bssid_match(wpa_s,
  1369. res->res[i]->bssid)) {
  1370. res->res[j++] = res->res[i];
  1371. } else {
  1372. os_free(res->res[i]);
  1373. res->res[i] = NULL;
  1374. }
  1375. }
  1376. if (res->num != j) {
  1377. wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
  1378. (int) (res->num - j));
  1379. res->num = j;
  1380. }
  1381. }
  1382. /**
  1383. * wpa_supplicant_get_scan_results - Get scan results
  1384. * @wpa_s: Pointer to wpa_supplicant data
  1385. * @info: Information about what was scanned or %NULL if not available
  1386. * @new_scan: Whether a new scan was performed
  1387. * Returns: Scan results, %NULL on failure
  1388. *
  1389. * This function request the current scan results from the driver and updates
  1390. * the local BSS list wpa_s->bss. The caller is responsible for freeing the
  1391. * results with wpa_scan_results_free().
  1392. */
  1393. struct wpa_scan_results *
  1394. wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
  1395. struct scan_info *info, int new_scan)
  1396. {
  1397. struct wpa_scan_results *scan_res;
  1398. size_t i;
  1399. int (*compar)(const void *, const void *) = wpa_scan_result_compar;
  1400. scan_res = wpa_drv_get_scan_results2(wpa_s);
  1401. if (scan_res == NULL) {
  1402. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
  1403. return NULL;
  1404. }
  1405. if (scan_res->fetch_time.sec == 0) {
  1406. /*
  1407. * Make sure we have a valid timestamp if the driver wrapper
  1408. * does not set this.
  1409. */
  1410. os_get_reltime(&scan_res->fetch_time);
  1411. }
  1412. filter_scan_res(wpa_s, scan_res);
  1413. #ifdef CONFIG_WPS
  1414. if (wpas_wps_searching(wpa_s)) {
  1415. wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
  1416. "provisioning rules");
  1417. compar = wpa_scan_result_wps_compar;
  1418. }
  1419. #endif /* CONFIG_WPS */
  1420. qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
  1421. compar);
  1422. dump_scan_res(scan_res);
  1423. wpa_bss_update_start(wpa_s);
  1424. for (i = 0; i < scan_res->num; i++)
  1425. wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
  1426. &scan_res->fetch_time);
  1427. wpa_bss_update_end(wpa_s, info, new_scan);
  1428. return scan_res;
  1429. }
  1430. /**
  1431. * wpa_supplicant_update_scan_results - Update scan results from the driver
  1432. * @wpa_s: Pointer to wpa_supplicant data
  1433. * Returns: 0 on success, -1 on failure
  1434. *
  1435. * This function updates the BSS table within wpa_supplicant based on the
  1436. * currently available scan results from the driver without requesting a new
  1437. * scan. This is used in cases where the driver indicates an association
  1438. * (including roaming within ESS) and wpa_supplicant does not yet have the
  1439. * needed information to complete the connection (e.g., to perform validation
  1440. * steps in 4-way handshake).
  1441. */
  1442. int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
  1443. {
  1444. struct wpa_scan_results *scan_res;
  1445. scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
  1446. if (scan_res == NULL)
  1447. return -1;
  1448. wpa_scan_results_free(scan_res);
  1449. return 0;
  1450. }
  1451. /**
  1452. * scan_only_handler - Reports scan results
  1453. */
  1454. void scan_only_handler(struct wpa_supplicant *wpa_s,
  1455. struct wpa_scan_results *scan_res)
  1456. {
  1457. wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
  1458. if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
  1459. wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
  1460. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
  1461. wpa_s->manual_scan_id);
  1462. wpa_s->manual_scan_use_id = 0;
  1463. } else {
  1464. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
  1465. }
  1466. wpas_notify_scan_results(wpa_s);
  1467. wpas_notify_scan_done(wpa_s, 1);
  1468. if (wpa_s->scan_work) {
  1469. struct wpa_radio_work *work = wpa_s->scan_work;
  1470. wpa_s->scan_work = NULL;
  1471. radio_work_done(work);
  1472. }
  1473. }
  1474. int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
  1475. {
  1476. return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
  1477. }
  1478. struct wpa_driver_scan_params *
  1479. wpa_scan_clone_params(const struct wpa_driver_scan_params *src)
  1480. {
  1481. struct wpa_driver_scan_params *params;
  1482. size_t i;
  1483. u8 *n;
  1484. params = os_zalloc(sizeof(*params));
  1485. if (params == NULL)
  1486. return NULL;
  1487. for (i = 0; i < src->num_ssids; i++) {
  1488. if (src->ssids[i].ssid) {
  1489. n = os_malloc(src->ssids[i].ssid_len);
  1490. if (n == NULL)
  1491. goto failed;
  1492. os_memcpy(n, src->ssids[i].ssid,
  1493. src->ssids[i].ssid_len);
  1494. params->ssids[i].ssid = n;
  1495. params->ssids[i].ssid_len = src->ssids[i].ssid_len;
  1496. }
  1497. }
  1498. params->num_ssids = src->num_ssids;
  1499. if (src->extra_ies) {
  1500. n = os_malloc(src->extra_ies_len);
  1501. if (n == NULL)
  1502. goto failed;
  1503. os_memcpy(n, src->extra_ies, src->extra_ies_len);
  1504. params->extra_ies = n;
  1505. params->extra_ies_len = src->extra_ies_len;
  1506. }
  1507. if (src->freqs) {
  1508. int len = int_array_len(src->freqs);
  1509. params->freqs = os_malloc((len + 1) * sizeof(int));
  1510. if (params->freqs == NULL)
  1511. goto failed;
  1512. os_memcpy(params->freqs, src->freqs, (len + 1) * sizeof(int));
  1513. }
  1514. if (src->filter_ssids) {
  1515. params->filter_ssids = os_malloc(sizeof(params->filter_ssids) *
  1516. src->num_filter_ssids);
  1517. if (params->filter_ssids == NULL)
  1518. goto failed;
  1519. os_memcpy(params->filter_ssids, src->filter_ssids,
  1520. sizeof(params->filter_ssids) * src->num_filter_ssids);
  1521. params->num_filter_ssids = src->num_filter_ssids;
  1522. }
  1523. params->filter_rssi = src->filter_rssi;
  1524. params->p2p_probe = src->p2p_probe;
  1525. params->only_new_results = src->only_new_results;
  1526. return params;
  1527. failed:
  1528. wpa_scan_free_params(params);
  1529. return NULL;
  1530. }
  1531. void wpa_scan_free_params(struct wpa_driver_scan_params *params)
  1532. {
  1533. size_t i;
  1534. if (params == NULL)
  1535. return;
  1536. for (i = 0; i < params->num_ssids; i++)
  1537. os_free((u8 *) params->ssids[i].ssid);
  1538. os_free((u8 *) params->extra_ies);
  1539. os_free(params->freqs);
  1540. os_free(params->filter_ssids);
  1541. os_free(params);
  1542. }