scan.c 53 KB

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