scan.c 55 KB

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