scan.c 48 KB

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