scan.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868
  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. 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 && os_strstr(ssid->eap.phase1, "pbc=1"))
  53. return 2;
  54. }
  55. #ifdef CONFIG_P2P
  56. if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p &&
  57. !wpa_s->conf->p2p_disabled) {
  58. wpa_s->wps->dev.p2p = 1;
  59. if (!wps) {
  60. wps = 1;
  61. *req_type = WPS_REQ_ENROLLEE_INFO;
  62. }
  63. }
  64. #endif /* CONFIG_P2P */
  65. return wps;
  66. }
  67. #endif /* CONFIG_WPS */
  68. /**
  69. * wpa_supplicant_enabled_networks - Check whether there are enabled networks
  70. * @wpa_s: Pointer to wpa_supplicant data
  71. * Returns: 0 if no networks are enabled, >0 if networks are enabled
  72. *
  73. * This function is used to figure out whether any networks (or Interworking
  74. * with enabled credentials and auto_interworking) are present in the current
  75. * configuration.
  76. */
  77. int wpa_supplicant_enabled_networks(struct wpa_supplicant *wpa_s)
  78. {
  79. struct wpa_ssid *ssid = wpa_s->conf->ssid;
  80. int count = 0, disabled = 0;
  81. if (wpa_s->p2p_mgmt)
  82. return 0; /* no normal network profiles on p2p_mgmt interface */
  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. int min_temp_disabled = 0;
  103. while (ssid) {
  104. if (!wpas_network_disabled(wpa_s, ssid)) {
  105. int temp_disabled = wpas_temp_disabled(wpa_s, ssid);
  106. if (temp_disabled <= 0)
  107. break;
  108. if (!min_temp_disabled ||
  109. temp_disabled < min_temp_disabled)
  110. min_temp_disabled = temp_disabled;
  111. }
  112. ssid = ssid->next;
  113. }
  114. /* ap_scan=2 mode - try to associate with each SSID. */
  115. if (ssid == NULL) {
  116. wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
  117. "end of scan list - go back to beginning");
  118. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  119. wpa_supplicant_req_scan(wpa_s, min_temp_disabled, 0);
  120. return;
  121. }
  122. if (ssid->next) {
  123. /* Continue from the next SSID on the next attempt. */
  124. wpa_s->prev_scan_ssid = ssid;
  125. } else {
  126. /* Start from the beginning of the SSID list. */
  127. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  128. }
  129. wpa_supplicant_associate(wpa_s, NULL, ssid);
  130. }
  131. static void wpas_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
  132. {
  133. struct wpa_supplicant *wpa_s = work->wpa_s;
  134. struct wpa_driver_scan_params *params = work->ctx;
  135. int ret;
  136. if (deinit) {
  137. if (!work->started) {
  138. wpa_scan_free_params(params);
  139. return;
  140. }
  141. wpa_supplicant_notify_scanning(wpa_s, 0);
  142. wpas_notify_scan_done(wpa_s, 0);
  143. wpa_s->scan_work = NULL;
  144. return;
  145. }
  146. if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
  147. wpa_msg(wpa_s, MSG_INFO,
  148. "Failed to assign random MAC address for a scan");
  149. wpa_scan_free_params(params);
  150. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=-1");
  151. radio_work_done(work);
  152. return;
  153. }
  154. wpa_supplicant_notify_scanning(wpa_s, 1);
  155. if (wpa_s->clear_driver_scan_cache) {
  156. wpa_printf(MSG_DEBUG,
  157. "Request driver to clear scan cache due to local BSS flush");
  158. params->only_new_results = 1;
  159. }
  160. ret = wpa_drv_scan(wpa_s, params);
  161. /*
  162. * Store the obtained vendor scan cookie (if any) in wpa_s context.
  163. * The current design is to allow only one scan request on each
  164. * interface, hence having this scan cookie stored in wpa_s context is
  165. * fine for now.
  166. *
  167. * Revisit this logic if concurrent scan operations per interface
  168. * is supported.
  169. */
  170. if (ret == 0)
  171. wpa_s->curr_scan_cookie = params->scan_cookie;
  172. wpa_scan_free_params(params);
  173. work->ctx = NULL;
  174. if (ret) {
  175. int retry = wpa_s->last_scan_req != MANUAL_SCAN_REQ;
  176. if (wpa_s->disconnected)
  177. retry = 0;
  178. wpa_supplicant_notify_scanning(wpa_s, 0);
  179. wpas_notify_scan_done(wpa_s, 0);
  180. if (wpa_s->wpa_state == WPA_SCANNING)
  181. wpa_supplicant_set_state(wpa_s,
  182. wpa_s->scan_prev_wpa_state);
  183. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=%d%s",
  184. ret, retry ? " retry=1" : "");
  185. radio_work_done(work);
  186. if (retry) {
  187. /* Restore scan_req since we will try to scan again */
  188. wpa_s->scan_req = wpa_s->last_scan_req;
  189. wpa_supplicant_req_scan(wpa_s, 1, 0);
  190. } else if (wpa_s->scan_res_handler) {
  191. /* Clear the scan_res_handler */
  192. wpa_s->scan_res_handler = NULL;
  193. }
  194. return;
  195. }
  196. os_get_reltime(&wpa_s->scan_trigger_time);
  197. wpa_s->scan_runs++;
  198. wpa_s->normal_scans++;
  199. wpa_s->own_scan_requested = 1;
  200. wpa_s->clear_driver_scan_cache = 0;
  201. wpa_s->scan_work = work;
  202. }
  203. /**
  204. * wpa_supplicant_trigger_scan - Request driver to start a scan
  205. * @wpa_s: Pointer to wpa_supplicant data
  206. * @params: Scan parameters
  207. * Returns: 0 on success, -1 on failure
  208. */
  209. int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
  210. struct wpa_driver_scan_params *params)
  211. {
  212. struct wpa_driver_scan_params *ctx;
  213. if (wpa_s->scan_work) {
  214. wpa_dbg(wpa_s, MSG_INFO, "Reject scan trigger since one is already pending");
  215. return -1;
  216. }
  217. ctx = wpa_scan_clone_params(params);
  218. if (!ctx ||
  219. radio_add_work(wpa_s, 0, "scan", 0, wpas_trigger_scan_cb, ctx) < 0)
  220. {
  221. wpa_scan_free_params(ctx);
  222. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=-1");
  223. return -1;
  224. }
  225. return 0;
  226. }
  227. static void
  228. wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  229. {
  230. struct wpa_supplicant *wpa_s = eloop_ctx;
  231. wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
  232. if (wpa_supplicant_req_sched_scan(wpa_s))
  233. wpa_supplicant_req_scan(wpa_s, 0, 0);
  234. }
  235. static void
  236. wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  237. {
  238. struct wpa_supplicant *wpa_s = eloop_ctx;
  239. wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
  240. wpa_s->sched_scan_timed_out = 1;
  241. wpa_supplicant_cancel_sched_scan(wpa_s);
  242. }
  243. static int
  244. wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
  245. struct wpa_driver_scan_params *params)
  246. {
  247. int ret;
  248. wpa_supplicant_notify_scanning(wpa_s, 1);
  249. ret = wpa_drv_sched_scan(wpa_s, params);
  250. if (ret)
  251. wpa_supplicant_notify_scanning(wpa_s, 0);
  252. else
  253. wpa_s->sched_scanning = 1;
  254. return ret;
  255. }
  256. static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
  257. {
  258. int ret;
  259. ret = wpa_drv_stop_sched_scan(wpa_s);
  260. if (ret) {
  261. wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
  262. /* TODO: what to do if stopping fails? */
  263. return -1;
  264. }
  265. return ret;
  266. }
  267. static struct wpa_driver_scan_filter *
  268. wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
  269. {
  270. struct wpa_driver_scan_filter *ssids;
  271. struct wpa_ssid *ssid;
  272. size_t count;
  273. *num_ssids = 0;
  274. if (!conf->filter_ssids)
  275. return NULL;
  276. for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
  277. if (ssid->ssid && ssid->ssid_len)
  278. count++;
  279. }
  280. if (count == 0)
  281. return NULL;
  282. ssids = os_calloc(count, sizeof(struct wpa_driver_scan_filter));
  283. if (ssids == NULL)
  284. return NULL;
  285. for (ssid = conf->ssid; ssid; ssid = ssid->next) {
  286. if (!ssid->ssid || !ssid->ssid_len)
  287. continue;
  288. os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
  289. ssids[*num_ssids].ssid_len = ssid->ssid_len;
  290. (*num_ssids)++;
  291. }
  292. return ssids;
  293. }
  294. static void wpa_supplicant_optimize_freqs(
  295. struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
  296. {
  297. #ifdef CONFIG_P2P
  298. if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
  299. wpa_s->go_params) {
  300. /* Optimize provisioning state scan based on GO information */
  301. if (wpa_s->p2p_in_provisioning < 5 &&
  302. wpa_s->go_params->freq > 0) {
  303. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
  304. "preferred frequency %d MHz",
  305. wpa_s->go_params->freq);
  306. params->freqs = os_calloc(2, sizeof(int));
  307. if (params->freqs)
  308. params->freqs[0] = wpa_s->go_params->freq;
  309. } else if (wpa_s->p2p_in_provisioning < 8 &&
  310. wpa_s->go_params->freq_list[0]) {
  311. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
  312. "channels");
  313. int_array_concat(&params->freqs,
  314. wpa_s->go_params->freq_list);
  315. if (params->freqs)
  316. int_array_sort_unique(params->freqs);
  317. }
  318. wpa_s->p2p_in_provisioning++;
  319. }
  320. if (params->freqs == NULL && wpa_s->p2p_in_invitation) {
  321. /*
  322. * Optimize scan based on GO information during persistent
  323. * group reinvocation
  324. */
  325. if (wpa_s->p2p_in_invitation < 5 &&
  326. wpa_s->p2p_invite_go_freq > 0) {
  327. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO preferred frequency %d MHz during invitation",
  328. wpa_s->p2p_invite_go_freq);
  329. params->freqs = os_calloc(2, sizeof(int));
  330. if (params->freqs)
  331. params->freqs[0] = wpa_s->p2p_invite_go_freq;
  332. }
  333. wpa_s->p2p_in_invitation++;
  334. if (wpa_s->p2p_in_invitation > 20) {
  335. /*
  336. * This should not really happen since the variable is
  337. * cleared on group removal, but if it does happen, make
  338. * sure we do not get stuck in special invitation scan
  339. * mode.
  340. */
  341. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Clear p2p_in_invitation");
  342. wpa_s->p2p_in_invitation = 0;
  343. }
  344. }
  345. #endif /* CONFIG_P2P */
  346. #ifdef CONFIG_WPS
  347. if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
  348. /*
  349. * Optimize post-provisioning scan based on channel used
  350. * during provisioning.
  351. */
  352. wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
  353. "that was used during provisioning", wpa_s->wps_freq);
  354. params->freqs = os_calloc(2, sizeof(int));
  355. if (params->freqs)
  356. params->freqs[0] = wpa_s->wps_freq;
  357. wpa_s->after_wps--;
  358. } else if (wpa_s->after_wps)
  359. wpa_s->after_wps--;
  360. if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
  361. {
  362. /* Optimize provisioning scan based on already known channel */
  363. wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
  364. wpa_s->wps_freq);
  365. params->freqs = os_calloc(2, sizeof(int));
  366. if (params->freqs)
  367. params->freqs[0] = wpa_s->wps_freq;
  368. wpa_s->known_wps_freq = 0; /* only do this once */
  369. }
  370. #endif /* CONFIG_WPS */
  371. }
  372. #ifdef CONFIG_INTERWORKING
  373. static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
  374. struct wpabuf *buf)
  375. {
  376. wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
  377. wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
  378. 1 + ETH_ALEN);
  379. wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
  380. /* No Venue Info */
  381. if (!is_zero_ether_addr(wpa_s->conf->hessid))
  382. wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
  383. }
  384. #endif /* CONFIG_INTERWORKING */
  385. void wpa_supplicant_set_default_scan_ies(struct wpa_supplicant *wpa_s)
  386. {
  387. struct wpabuf *default_ies = NULL;
  388. u8 ext_capab[18];
  389. int ext_capab_len;
  390. enum wpa_driver_if_type type = WPA_IF_STATION;
  391. #ifdef CONFIG_P2P
  392. if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT)
  393. type = WPA_IF_P2P_CLIENT;
  394. #endif /* CONFIG_P2P */
  395. wpa_drv_get_ext_capa(wpa_s, type);
  396. ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
  397. sizeof(ext_capab));
  398. if (ext_capab_len > 0 &&
  399. wpabuf_resize(&default_ies, ext_capab_len) == 0)
  400. wpabuf_put_data(default_ies, ext_capab, ext_capab_len);
  401. #ifdef CONFIG_MBO
  402. /* Send cellular capabilities for potential MBO STAs */
  403. if (wpabuf_resize(&default_ies, 9) == 0)
  404. wpas_mbo_scan_ie(wpa_s, default_ies);
  405. #endif /* CONFIG_MBO */
  406. if (default_ies)
  407. wpa_drv_set_default_scan_ies(wpa_s, wpabuf_head(default_ies),
  408. wpabuf_len(default_ies));
  409. wpabuf_free(default_ies);
  410. }
  411. static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
  412. {
  413. struct wpabuf *extra_ie = NULL;
  414. u8 ext_capab[18];
  415. int ext_capab_len;
  416. #ifdef CONFIG_WPS
  417. int wps = 0;
  418. enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
  419. #endif /* CONFIG_WPS */
  420. #ifdef CONFIG_P2P
  421. if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT)
  422. wpa_drv_get_ext_capa(wpa_s, WPA_IF_P2P_CLIENT);
  423. else
  424. #endif /* CONFIG_P2P */
  425. wpa_drv_get_ext_capa(wpa_s, WPA_IF_STATION);
  426. ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
  427. sizeof(ext_capab));
  428. if (ext_capab_len > 0 &&
  429. wpabuf_resize(&extra_ie, ext_capab_len) == 0)
  430. wpabuf_put_data(extra_ie, ext_capab, ext_capab_len);
  431. #ifdef CONFIG_INTERWORKING
  432. if (wpa_s->conf->interworking &&
  433. wpabuf_resize(&extra_ie, 100) == 0)
  434. wpas_add_interworking_elements(wpa_s, extra_ie);
  435. #endif /* CONFIG_INTERWORKING */
  436. #ifdef CONFIG_WPS
  437. wps = wpas_wps_in_use(wpa_s, &req_type);
  438. if (wps) {
  439. struct wpabuf *wps_ie;
  440. wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
  441. DEV_PW_DEFAULT,
  442. &wpa_s->wps->dev,
  443. wpa_s->wps->uuid, req_type,
  444. 0, NULL);
  445. if (wps_ie) {
  446. if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
  447. wpabuf_put_buf(extra_ie, wps_ie);
  448. wpabuf_free(wps_ie);
  449. }
  450. }
  451. #ifdef CONFIG_P2P
  452. if (wps) {
  453. size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  454. if (wpabuf_resize(&extra_ie, ielen) == 0)
  455. wpas_p2p_scan_ie(wpa_s, extra_ie);
  456. }
  457. #endif /* CONFIG_P2P */
  458. wpa_supplicant_mesh_add_scan_ie(wpa_s, &extra_ie);
  459. #endif /* CONFIG_WPS */
  460. #ifdef CONFIG_HS20
  461. if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 7) == 0)
  462. wpas_hs20_add_indication(extra_ie, -1);
  463. #endif /* CONFIG_HS20 */
  464. #ifdef CONFIG_FST
  465. if (wpa_s->fst_ies &&
  466. wpabuf_resize(&extra_ie, wpabuf_len(wpa_s->fst_ies)) == 0)
  467. wpabuf_put_buf(extra_ie, wpa_s->fst_ies);
  468. #endif /* CONFIG_FST */
  469. #ifdef CONFIG_MBO
  470. /* Send cellular capabilities for potential MBO STAs */
  471. if (wpabuf_resize(&extra_ie, 9) == 0)
  472. wpas_mbo_scan_ie(wpa_s, extra_ie);
  473. #endif /* CONFIG_MBO */
  474. if (wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ]) {
  475. struct wpabuf *buf = wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ];
  476. if (wpabuf_resize(&extra_ie, wpabuf_len(buf)) == 0)
  477. wpabuf_put_buf(extra_ie, buf);
  478. }
  479. return extra_ie;
  480. }
  481. #ifdef CONFIG_P2P
  482. /*
  483. * Check whether there are any enabled networks or credentials that could be
  484. * used for a non-P2P connection.
  485. */
  486. static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
  487. {
  488. struct wpa_ssid *ssid;
  489. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  490. if (wpas_network_disabled(wpa_s, ssid))
  491. continue;
  492. if (!ssid->p2p_group)
  493. return 1;
  494. }
  495. if (wpa_s->conf->cred && wpa_s->conf->interworking &&
  496. wpa_s->conf->auto_interworking)
  497. return 1;
  498. return 0;
  499. }
  500. #endif /* CONFIG_P2P */
  501. static void wpa_setband_scan_freqs_list(struct wpa_supplicant *wpa_s,
  502. enum hostapd_hw_mode band,
  503. struct wpa_driver_scan_params *params)
  504. {
  505. /* Include only supported channels for the specified band */
  506. struct hostapd_hw_modes *mode;
  507. int count, i;
  508. mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band);
  509. if (mode == NULL) {
  510. /* No channels supported in this band - use empty list */
  511. params->freqs = os_zalloc(sizeof(int));
  512. return;
  513. }
  514. params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
  515. if (params->freqs == NULL)
  516. return;
  517. for (count = 0, i = 0; i < mode->num_channels; i++) {
  518. if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
  519. continue;
  520. params->freqs[count++] = mode->channels[i].freq;
  521. }
  522. }
  523. static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
  524. struct wpa_driver_scan_params *params)
  525. {
  526. if (wpa_s->hw.modes == NULL)
  527. return; /* unknown what channels the driver supports */
  528. if (params->freqs)
  529. return; /* already using a limited channel set */
  530. if (wpa_s->setband == WPA_SETBAND_5G)
  531. wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
  532. params);
  533. else if (wpa_s->setband == WPA_SETBAND_2G)
  534. wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G,
  535. params);
  536. }
  537. static void wpa_set_scan_ssids(struct wpa_supplicant *wpa_s,
  538. struct wpa_driver_scan_params *params,
  539. size_t max_ssids)
  540. {
  541. unsigned int i;
  542. struct wpa_ssid *ssid;
  543. /*
  544. * For devices with max_ssids greater than 1, leave the last slot empty
  545. * for adding the wildcard scan entry.
  546. */
  547. max_ssids = max_ssids > 1 ? max_ssids - 1 : max_ssids;
  548. for (i = 0; i < wpa_s->scan_id_count; i++) {
  549. unsigned int j;
  550. ssid = wpa_config_get_network(wpa_s->conf, wpa_s->scan_id[i]);
  551. if (!ssid || !ssid->scan_ssid)
  552. continue;
  553. for (j = 0; j < params->num_ssids; j++) {
  554. if (params->ssids[j].ssid_len == ssid->ssid_len &&
  555. params->ssids[j].ssid &&
  556. os_memcmp(params->ssids[j].ssid, ssid->ssid,
  557. ssid->ssid_len) == 0)
  558. break;
  559. }
  560. if (j < params->num_ssids)
  561. continue; /* already in the list */
  562. if (params->num_ssids + 1 > max_ssids) {
  563. wpa_printf(MSG_DEBUG,
  564. "Over max scan SSIDs for manual request");
  565. break;
  566. }
  567. wpa_printf(MSG_DEBUG, "Scan SSID (manual request): %s",
  568. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  569. params->ssids[params->num_ssids].ssid = ssid->ssid;
  570. params->ssids[params->num_ssids].ssid_len = ssid->ssid_len;
  571. params->num_ssids++;
  572. }
  573. wpa_s->scan_id_count = 0;
  574. }
  575. static int wpa_set_ssids_from_scan_req(struct wpa_supplicant *wpa_s,
  576. struct wpa_driver_scan_params *params,
  577. size_t max_ssids)
  578. {
  579. unsigned int i;
  580. if (wpa_s->ssids_from_scan_req == NULL ||
  581. wpa_s->num_ssids_from_scan_req == 0)
  582. return 0;
  583. if (wpa_s->num_ssids_from_scan_req > max_ssids) {
  584. wpa_s->num_ssids_from_scan_req = max_ssids;
  585. wpa_printf(MSG_DEBUG, "Over max scan SSIDs from scan req: %u",
  586. (unsigned int) max_ssids);
  587. }
  588. for (i = 0; i < wpa_s->num_ssids_from_scan_req; i++) {
  589. params->ssids[i].ssid = wpa_s->ssids_from_scan_req[i].ssid;
  590. params->ssids[i].ssid_len =
  591. wpa_s->ssids_from_scan_req[i].ssid_len;
  592. wpa_hexdump_ascii(MSG_DEBUG, "specific SSID",
  593. params->ssids[i].ssid,
  594. params->ssids[i].ssid_len);
  595. }
  596. params->num_ssids = wpa_s->num_ssids_from_scan_req;
  597. wpa_s->num_ssids_from_scan_req = 0;
  598. return 1;
  599. }
  600. static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
  601. {
  602. struct wpa_supplicant *wpa_s = eloop_ctx;
  603. struct wpa_ssid *ssid;
  604. int ret, p2p_in_prog;
  605. struct wpabuf *extra_ie = NULL;
  606. struct wpa_driver_scan_params params;
  607. struct wpa_driver_scan_params *scan_params;
  608. size_t max_ssids;
  609. int connect_without_scan = 0;
  610. wpa_s->ignore_post_flush_scan_res = 0;
  611. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  612. wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
  613. return;
  614. }
  615. if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
  616. wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
  617. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  618. return;
  619. }
  620. if (wpa_s->scanning) {
  621. /*
  622. * If we are already in scanning state, we shall reschedule the
  623. * the incoming scan request.
  624. */
  625. wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req");
  626. wpa_supplicant_req_scan(wpa_s, 1, 0);
  627. return;
  628. }
  629. if (!wpa_supplicant_enabled_networks(wpa_s) &&
  630. wpa_s->scan_req == NORMAL_SCAN_REQ) {
  631. wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
  632. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  633. return;
  634. }
  635. if (wpa_s->conf->ap_scan != 0 &&
  636. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
  637. wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
  638. "overriding ap_scan configuration");
  639. wpa_s->conf->ap_scan = 0;
  640. wpas_notify_ap_scan_changed(wpa_s);
  641. }
  642. if (wpa_s->conf->ap_scan == 0) {
  643. wpa_supplicant_gen_assoc_event(wpa_s);
  644. return;
  645. }
  646. ssid = NULL;
  647. if (wpa_s->scan_req != MANUAL_SCAN_REQ &&
  648. wpa_s->connect_without_scan) {
  649. connect_without_scan = 1;
  650. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  651. if (ssid == wpa_s->connect_without_scan)
  652. break;
  653. }
  654. }
  655. p2p_in_prog = wpas_p2p_in_progress(wpa_s);
  656. if (p2p_in_prog && p2p_in_prog != 2 &&
  657. (!ssid ||
  658. (ssid->mode != WPAS_MODE_AP && ssid->mode != WPAS_MODE_P2P_GO))) {
  659. wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan while P2P operation is in progress");
  660. wpa_supplicant_req_scan(wpa_s, 5, 0);
  661. return;
  662. }
  663. /*
  664. * Don't cancel the scan based on ongoing PNO; defer it. Some scans are
  665. * used for changing modes inside wpa_supplicant (roaming,
  666. * auto-reconnect, etc). Discarding the scan might hurt these processes.
  667. * The normal use case for PNO is to suspend the host immediately after
  668. * starting PNO, so the periodic 100 ms attempts to run the scan do not
  669. * normally happen in practice multiple times, i.e., this is simply
  670. * restarting scanning once the host is woken up and PNO stopped.
  671. */
  672. if (wpa_s->pno || wpa_s->pno_sched_pending) {
  673. wpa_dbg(wpa_s, MSG_DEBUG, "Defer scan - PNO is in progress");
  674. wpa_supplicant_req_scan(wpa_s, 0, 100000);
  675. return;
  676. }
  677. if (wpa_s->conf->ap_scan == 2)
  678. max_ssids = 1;
  679. else {
  680. max_ssids = wpa_s->max_scan_ssids;
  681. if (max_ssids > WPAS_MAX_SCAN_SSIDS)
  682. max_ssids = WPAS_MAX_SCAN_SSIDS;
  683. }
  684. wpa_s->last_scan_req = wpa_s->scan_req;
  685. wpa_s->scan_req = NORMAL_SCAN_REQ;
  686. if (connect_without_scan) {
  687. wpa_s->connect_without_scan = NULL;
  688. if (ssid) {
  689. wpa_printf(MSG_DEBUG, "Start a pre-selected network "
  690. "without scan step");
  691. wpa_supplicant_associate(wpa_s, NULL, ssid);
  692. return;
  693. }
  694. }
  695. os_memset(&params, 0, sizeof(params));
  696. wpa_s->scan_prev_wpa_state = wpa_s->wpa_state;
  697. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  698. wpa_s->wpa_state == WPA_INACTIVE)
  699. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  700. /*
  701. * If autoscan has set its own scanning parameters
  702. */
  703. if (wpa_s->autoscan_params != NULL) {
  704. scan_params = wpa_s->autoscan_params;
  705. goto scan;
  706. }
  707. if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
  708. wpa_set_ssids_from_scan_req(wpa_s, &params, max_ssids)) {
  709. wpa_printf(MSG_DEBUG, "Use specific SSIDs from SCAN command");
  710. goto ssid_list_set;
  711. }
  712. #ifdef CONFIG_P2P
  713. if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
  714. wpa_s->go_params && !wpa_s->conf->passive_scan) {
  715. wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
  716. wpa_s->p2p_in_provisioning,
  717. wpa_s->show_group_started);
  718. params.ssids[0].ssid = wpa_s->go_params->ssid;
  719. params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
  720. params.num_ssids = 1;
  721. goto ssid_list_set;
  722. }
  723. if (wpa_s->p2p_in_invitation) {
  724. if (wpa_s->current_ssid) {
  725. wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during invitation");
  726. params.ssids[0].ssid = wpa_s->current_ssid->ssid;
  727. params.ssids[0].ssid_len =
  728. wpa_s->current_ssid->ssid_len;
  729. params.num_ssids = 1;
  730. } else {
  731. wpa_printf(MSG_DEBUG, "P2P: No specific SSID known for scan during invitation");
  732. }
  733. goto ssid_list_set;
  734. }
  735. #endif /* CONFIG_P2P */
  736. /* Find the starting point from which to continue scanning */
  737. ssid = wpa_s->conf->ssid;
  738. if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
  739. while (ssid) {
  740. if (ssid == wpa_s->prev_scan_ssid) {
  741. ssid = ssid->next;
  742. break;
  743. }
  744. ssid = ssid->next;
  745. }
  746. }
  747. if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
  748. #ifdef CONFIG_AP
  749. !wpa_s->ap_iface &&
  750. #endif /* CONFIG_AP */
  751. wpa_s->conf->ap_scan == 2) {
  752. wpa_s->connect_without_scan = NULL;
  753. wpa_s->prev_scan_wildcard = 0;
  754. wpa_supplicant_assoc_try(wpa_s, ssid);
  755. return;
  756. } else if (wpa_s->conf->ap_scan == 2) {
  757. /*
  758. * User-initiated scan request in ap_scan == 2; scan with
  759. * wildcard SSID.
  760. */
  761. ssid = NULL;
  762. } else if (wpa_s->reattach && wpa_s->current_ssid != NULL) {
  763. /*
  764. * Perform single-channel single-SSID scan for
  765. * reassociate-to-same-BSS operation.
  766. */
  767. /* Setup SSID */
  768. ssid = wpa_s->current_ssid;
  769. wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
  770. ssid->ssid, ssid->ssid_len);
  771. params.ssids[0].ssid = ssid->ssid;
  772. params.ssids[0].ssid_len = ssid->ssid_len;
  773. params.num_ssids = 1;
  774. /*
  775. * Allocate memory for frequency array, allocate one extra
  776. * slot for the zero-terminator.
  777. */
  778. params.freqs = os_malloc(sizeof(int) * 2);
  779. if (params.freqs) {
  780. params.freqs[0] = wpa_s->assoc_freq;
  781. params.freqs[1] = 0;
  782. }
  783. /*
  784. * Reset the reattach flag so that we fall back to full scan if
  785. * this scan fails.
  786. */
  787. wpa_s->reattach = 0;
  788. } else {
  789. struct wpa_ssid *start = ssid, *tssid;
  790. int freqs_set = 0;
  791. if (ssid == NULL && max_ssids > 1)
  792. ssid = wpa_s->conf->ssid;
  793. while (ssid) {
  794. if (!wpas_network_disabled(wpa_s, ssid) &&
  795. ssid->scan_ssid) {
  796. wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
  797. ssid->ssid, ssid->ssid_len);
  798. params.ssids[params.num_ssids].ssid =
  799. ssid->ssid;
  800. params.ssids[params.num_ssids].ssid_len =
  801. ssid->ssid_len;
  802. params.num_ssids++;
  803. if (params.num_ssids + 1 >= max_ssids)
  804. break;
  805. }
  806. ssid = ssid->next;
  807. if (ssid == start)
  808. break;
  809. if (ssid == NULL && max_ssids > 1 &&
  810. start != wpa_s->conf->ssid)
  811. ssid = wpa_s->conf->ssid;
  812. }
  813. if (wpa_s->scan_id_count &&
  814. wpa_s->last_scan_req == MANUAL_SCAN_REQ)
  815. wpa_set_scan_ssids(wpa_s, &params, max_ssids);
  816. for (tssid = wpa_s->conf->ssid;
  817. wpa_s->last_scan_req != MANUAL_SCAN_REQ && tssid;
  818. tssid = tssid->next) {
  819. if (wpas_network_disabled(wpa_s, tssid))
  820. continue;
  821. if ((params.freqs || !freqs_set) && tssid->scan_freq) {
  822. int_array_concat(&params.freqs,
  823. tssid->scan_freq);
  824. } else {
  825. os_free(params.freqs);
  826. params.freqs = NULL;
  827. }
  828. freqs_set = 1;
  829. }
  830. int_array_sort_unique(params.freqs);
  831. }
  832. if (ssid && max_ssids == 1) {
  833. /*
  834. * If the driver is limited to 1 SSID at a time interleave
  835. * wildcard SSID scans with specific SSID scans to avoid
  836. * waiting a long time for a wildcard scan.
  837. */
  838. if (!wpa_s->prev_scan_wildcard) {
  839. params.ssids[0].ssid = NULL;
  840. params.ssids[0].ssid_len = 0;
  841. wpa_s->prev_scan_wildcard = 1;
  842. wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
  843. "wildcard SSID (Interleave with specific)");
  844. } else {
  845. wpa_s->prev_scan_ssid = ssid;
  846. wpa_s->prev_scan_wildcard = 0;
  847. wpa_dbg(wpa_s, MSG_DEBUG,
  848. "Starting AP scan for specific SSID: %s",
  849. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  850. }
  851. } else if (ssid) {
  852. /* max_ssids > 1 */
  853. wpa_s->prev_scan_ssid = ssid;
  854. wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
  855. "the scan request");
  856. params.num_ssids++;
  857. } else if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
  858. wpa_s->manual_scan_passive && params.num_ssids == 0) {
  859. wpa_dbg(wpa_s, MSG_DEBUG, "Use passive scan based on manual request");
  860. } else if (wpa_s->conf->passive_scan) {
  861. wpa_dbg(wpa_s, MSG_DEBUG,
  862. "Use passive scan based on configuration");
  863. } else {
  864. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  865. params.num_ssids++;
  866. wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
  867. "SSID");
  868. }
  869. ssid_list_set:
  870. wpa_supplicant_optimize_freqs(wpa_s, &params);
  871. extra_ie = wpa_supplicant_extra_ies(wpa_s);
  872. if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
  873. wpa_s->manual_scan_only_new) {
  874. wpa_printf(MSG_DEBUG,
  875. "Request driver to clear scan cache due to manual only_new=1 scan");
  876. params.only_new_results = 1;
  877. }
  878. if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
  879. wpa_s->manual_scan_freqs) {
  880. wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
  881. params.freqs = wpa_s->manual_scan_freqs;
  882. wpa_s->manual_scan_freqs = NULL;
  883. }
  884. if (params.freqs == NULL && wpa_s->select_network_scan_freqs) {
  885. wpa_dbg(wpa_s, MSG_DEBUG,
  886. "Limit select_network scan to specified channels");
  887. params.freqs = wpa_s->select_network_scan_freqs;
  888. wpa_s->select_network_scan_freqs = NULL;
  889. }
  890. if (params.freqs == NULL && wpa_s->next_scan_freqs) {
  891. wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
  892. "generated frequency list");
  893. params.freqs = wpa_s->next_scan_freqs;
  894. } else
  895. os_free(wpa_s->next_scan_freqs);
  896. wpa_s->next_scan_freqs = NULL;
  897. wpa_setband_scan_freqs(wpa_s, &params);
  898. /* See if user specified frequencies. If so, scan only those. */
  899. if (wpa_s->conf->freq_list && !params.freqs) {
  900. wpa_dbg(wpa_s, MSG_DEBUG,
  901. "Optimize scan based on conf->freq_list");
  902. int_array_concat(&params.freqs, wpa_s->conf->freq_list);
  903. }
  904. /* Use current associated channel? */
  905. if (wpa_s->conf->scan_cur_freq && !params.freqs) {
  906. unsigned int num = wpa_s->num_multichan_concurrent;
  907. params.freqs = os_calloc(num + 1, sizeof(int));
  908. if (params.freqs) {
  909. num = get_shared_radio_freqs(wpa_s, params.freqs, num);
  910. if (num > 0) {
  911. wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
  912. "current operating channels since "
  913. "scan_cur_freq is enabled");
  914. } else {
  915. os_free(params.freqs);
  916. params.freqs = NULL;
  917. }
  918. }
  919. }
  920. params.filter_ssids = wpa_supplicant_build_filter_ssids(
  921. wpa_s->conf, &params.num_filter_ssids);
  922. if (extra_ie) {
  923. params.extra_ies = wpabuf_head(extra_ie);
  924. params.extra_ies_len = wpabuf_len(extra_ie);
  925. }
  926. #ifdef CONFIG_P2P
  927. if (wpa_s->p2p_in_provisioning || wpa_s->p2p_in_invitation ||
  928. (wpa_s->show_group_started && wpa_s->go_params)) {
  929. /*
  930. * The interface may not yet be in P2P mode, so we have to
  931. * explicitly request P2P probe to disable CCK rates.
  932. */
  933. params.p2p_probe = 1;
  934. }
  935. #endif /* CONFIG_P2P */
  936. if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) &&
  937. wpa_s->wpa_state <= WPA_SCANNING) {
  938. params.mac_addr_rand = 1;
  939. if (wpa_s->mac_addr_scan) {
  940. params.mac_addr = wpa_s->mac_addr_scan;
  941. params.mac_addr_mask = wpa_s->mac_addr_scan + ETH_ALEN;
  942. }
  943. }
  944. if (!is_zero_ether_addr(wpa_s->next_scan_bssid)) {
  945. struct wpa_bss *bss;
  946. params.bssid = wpa_s->next_scan_bssid;
  947. bss = wpa_bss_get_bssid_latest(wpa_s, params.bssid);
  948. if (bss && bss->ssid_len && params.num_ssids == 1 &&
  949. params.ssids[0].ssid_len == 0) {
  950. params.ssids[0].ssid = bss->ssid;
  951. params.ssids[0].ssid_len = bss->ssid_len;
  952. wpa_dbg(wpa_s, MSG_DEBUG,
  953. "Scan a previously specified BSSID " MACSTR
  954. " and SSID %s",
  955. MAC2STR(params.bssid),
  956. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  957. } else {
  958. wpa_dbg(wpa_s, MSG_DEBUG,
  959. "Scan a previously specified BSSID " MACSTR,
  960. MAC2STR(params.bssid));
  961. }
  962. }
  963. scan_params = &params;
  964. scan:
  965. #ifdef CONFIG_P2P
  966. /*
  967. * If the driver does not support multi-channel concurrency and a
  968. * virtual interface that shares the same radio with the wpa_s interface
  969. * is operating there may not be need to scan other channels apart from
  970. * the current operating channel on the other virtual interface. Filter
  971. * out other channels in case we are trying to find a connection for a
  972. * station interface when we are not configured to prefer station
  973. * connection and a concurrent operation is already in process.
  974. */
  975. if (wpa_s->scan_for_connection &&
  976. wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
  977. !scan_params->freqs && !params.freqs &&
  978. wpas_is_p2p_prioritized(wpa_s) &&
  979. wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
  980. non_p2p_network_enabled(wpa_s)) {
  981. unsigned int num = wpa_s->num_multichan_concurrent;
  982. params.freqs = os_calloc(num + 1, sizeof(int));
  983. if (params.freqs) {
  984. num = get_shared_radio_freqs(wpa_s, params.freqs, num);
  985. if (num > 0 && num == wpa_s->num_multichan_concurrent) {
  986. wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
  987. } else {
  988. os_free(params.freqs);
  989. params.freqs = NULL;
  990. }
  991. }
  992. }
  993. #endif /* CONFIG_P2P */
  994. ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
  995. if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
  996. !wpa_s->manual_scan_freqs) {
  997. /* Restore manual_scan_freqs for the next attempt */
  998. wpa_s->manual_scan_freqs = params.freqs;
  999. params.freqs = NULL;
  1000. }
  1001. wpabuf_free(extra_ie);
  1002. os_free(params.freqs);
  1003. os_free(params.filter_ssids);
  1004. if (ret) {
  1005. wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
  1006. if (wpa_s->scan_prev_wpa_state != wpa_s->wpa_state)
  1007. wpa_supplicant_set_state(wpa_s,
  1008. wpa_s->scan_prev_wpa_state);
  1009. /* Restore scan_req since we will try to scan again */
  1010. wpa_s->scan_req = wpa_s->last_scan_req;
  1011. wpa_supplicant_req_scan(wpa_s, 1, 0);
  1012. } else {
  1013. wpa_s->scan_for_connection = 0;
  1014. #ifdef CONFIG_INTERWORKING
  1015. wpa_s->interworking_fast_assoc_tried = 0;
  1016. #endif /* CONFIG_INTERWORKING */
  1017. if (params.bssid)
  1018. os_memset(wpa_s->next_scan_bssid, 0, ETH_ALEN);
  1019. }
  1020. }
  1021. void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
  1022. {
  1023. struct os_reltime remaining, new_int;
  1024. int cancelled;
  1025. cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
  1026. &remaining);
  1027. new_int.sec = sec;
  1028. new_int.usec = 0;
  1029. if (cancelled && os_reltime_before(&remaining, &new_int)) {
  1030. new_int.sec = remaining.sec;
  1031. new_int.usec = remaining.usec;
  1032. }
  1033. if (cancelled) {
  1034. eloop_register_timeout(new_int.sec, new_int.usec,
  1035. wpa_supplicant_scan, wpa_s, NULL);
  1036. }
  1037. wpa_s->scan_interval = sec;
  1038. }
  1039. /**
  1040. * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
  1041. * @wpa_s: Pointer to wpa_supplicant data
  1042. * @sec: Number of seconds after which to scan
  1043. * @usec: Number of microseconds after which to scan
  1044. *
  1045. * This function is used to schedule a scan for neighboring access points after
  1046. * the specified time.
  1047. */
  1048. void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
  1049. {
  1050. int res;
  1051. if (wpa_s->p2p_mgmt) {
  1052. wpa_dbg(wpa_s, MSG_DEBUG,
  1053. "Ignore scan request (%d.%06d sec) on p2p_mgmt interface",
  1054. sec, usec);
  1055. return;
  1056. }
  1057. res = eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s,
  1058. NULL);
  1059. if (res == 1) {
  1060. wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d.%06d sec",
  1061. sec, usec);
  1062. } else if (res == 0) {
  1063. wpa_dbg(wpa_s, MSG_DEBUG, "Ignore new scan request for %d.%06d sec since an earlier request is scheduled to trigger sooner",
  1064. sec, usec);
  1065. } else {
  1066. wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d.%06d sec",
  1067. sec, usec);
  1068. eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
  1069. }
  1070. }
  1071. /**
  1072. * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
  1073. * @wpa_s: Pointer to wpa_supplicant data
  1074. * @sec: Number of seconds after which to scan
  1075. * @usec: Number of microseconds after which to scan
  1076. * Returns: 0 on success or -1 otherwise
  1077. *
  1078. * This function is used to schedule periodic scans for neighboring
  1079. * access points after the specified time.
  1080. */
  1081. int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
  1082. int sec, int usec)
  1083. {
  1084. if (!wpa_s->sched_scan_supported)
  1085. return -1;
  1086. eloop_register_timeout(sec, usec,
  1087. wpa_supplicant_delayed_sched_scan_timeout,
  1088. wpa_s, NULL);
  1089. return 0;
  1090. }
  1091. static void
  1092. wpa_scan_set_relative_rssi_params(struct wpa_supplicant *wpa_s,
  1093. struct wpa_driver_scan_params *params)
  1094. {
  1095. if (wpa_s->wpa_state != WPA_COMPLETED ||
  1096. !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SCHED_SCAN_RELATIVE_RSSI) ||
  1097. wpa_s->srp.relative_rssi_set == 0)
  1098. return;
  1099. params->relative_rssi_set = 1;
  1100. params->relative_rssi = wpa_s->srp.relative_rssi;
  1101. if (wpa_s->srp.relative_adjust_rssi == 0)
  1102. return;
  1103. params->relative_adjust_band = wpa_s->srp.relative_adjust_band;
  1104. params->relative_adjust_rssi = wpa_s->srp.relative_adjust_rssi;
  1105. }
  1106. /**
  1107. * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
  1108. * @wpa_s: Pointer to wpa_supplicant data
  1109. * Returns: 0 is sched_scan was started or -1 otherwise
  1110. *
  1111. * This function is used to schedule periodic scans for neighboring
  1112. * access points repeating the scan continuously.
  1113. */
  1114. int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
  1115. {
  1116. struct wpa_driver_scan_params params;
  1117. struct wpa_driver_scan_params *scan_params;
  1118. enum wpa_states prev_state;
  1119. struct wpa_ssid *ssid = NULL;
  1120. struct wpabuf *extra_ie = NULL;
  1121. int ret;
  1122. unsigned int max_sched_scan_ssids;
  1123. int wildcard = 0;
  1124. int need_ssids;
  1125. struct sched_scan_plan scan_plan;
  1126. if (!wpa_s->sched_scan_supported)
  1127. return -1;
  1128. if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
  1129. max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
  1130. else
  1131. max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
  1132. if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
  1133. return -1;
  1134. wpa_s->sched_scan_stop_req = 0;
  1135. if (wpa_s->sched_scanning) {
  1136. wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
  1137. return 0;
  1138. }
  1139. need_ssids = 0;
  1140. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  1141. if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
  1142. /* Use wildcard SSID to find this network */
  1143. wildcard = 1;
  1144. } else if (!wpas_network_disabled(wpa_s, ssid) &&
  1145. ssid->ssid_len)
  1146. need_ssids++;
  1147. #ifdef CONFIG_WPS
  1148. if (!wpas_network_disabled(wpa_s, ssid) &&
  1149. ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
  1150. /*
  1151. * Normal scan is more reliable and faster for WPS
  1152. * operations and since these are for short periods of
  1153. * time, the benefit of trying to use sched_scan would
  1154. * be limited.
  1155. */
  1156. wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
  1157. "sched_scan for WPS");
  1158. return -1;
  1159. }
  1160. #endif /* CONFIG_WPS */
  1161. }
  1162. if (wildcard)
  1163. need_ssids++;
  1164. if (wpa_s->normal_scans < 3 &&
  1165. (need_ssids <= wpa_s->max_scan_ssids ||
  1166. wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
  1167. /*
  1168. * When normal scan can speed up operations, use that for the
  1169. * first operations before starting the sched_scan to allow
  1170. * user space sleep more. We do this only if the normal scan
  1171. * has functionality that is suitable for this or if the
  1172. * sched_scan does not have better support for multiple SSIDs.
  1173. */
  1174. wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
  1175. "sched_scan for initial scans (normal_scans=%d)",
  1176. wpa_s->normal_scans);
  1177. return -1;
  1178. }
  1179. os_memset(&params, 0, sizeof(params));
  1180. /* If we can't allocate space for the filters, we just don't filter */
  1181. params.filter_ssids = os_calloc(wpa_s->max_match_sets,
  1182. sizeof(struct wpa_driver_scan_filter));
  1183. prev_state = wpa_s->wpa_state;
  1184. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  1185. wpa_s->wpa_state == WPA_INACTIVE)
  1186. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  1187. if (wpa_s->autoscan_params != NULL) {
  1188. scan_params = wpa_s->autoscan_params;
  1189. goto scan;
  1190. }
  1191. /* Find the starting point from which to continue scanning */
  1192. ssid = wpa_s->conf->ssid;
  1193. if (wpa_s->prev_sched_ssid) {
  1194. while (ssid) {
  1195. if (ssid == wpa_s->prev_sched_ssid) {
  1196. ssid = ssid->next;
  1197. break;
  1198. }
  1199. ssid = ssid->next;
  1200. }
  1201. }
  1202. if (!ssid || !wpa_s->prev_sched_ssid) {
  1203. wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
  1204. wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
  1205. wpa_s->first_sched_scan = 1;
  1206. ssid = wpa_s->conf->ssid;
  1207. wpa_s->prev_sched_ssid = ssid;
  1208. }
  1209. if (wildcard) {
  1210. wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
  1211. params.num_ssids++;
  1212. }
  1213. while (ssid) {
  1214. if (wpas_network_disabled(wpa_s, ssid))
  1215. goto next;
  1216. if (params.num_filter_ssids < wpa_s->max_match_sets &&
  1217. params.filter_ssids && ssid->ssid && ssid->ssid_len) {
  1218. wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
  1219. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  1220. os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
  1221. ssid->ssid, ssid->ssid_len);
  1222. params.filter_ssids[params.num_filter_ssids].ssid_len =
  1223. ssid->ssid_len;
  1224. params.num_filter_ssids++;
  1225. } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
  1226. {
  1227. wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
  1228. "filter for sched_scan - drop filter");
  1229. os_free(params.filter_ssids);
  1230. params.filter_ssids = NULL;
  1231. params.num_filter_ssids = 0;
  1232. }
  1233. if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
  1234. if (params.num_ssids == max_sched_scan_ssids)
  1235. break; /* only room for broadcast SSID */
  1236. wpa_dbg(wpa_s, MSG_DEBUG,
  1237. "add to active scan ssid: %s",
  1238. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  1239. params.ssids[params.num_ssids].ssid =
  1240. ssid->ssid;
  1241. params.ssids[params.num_ssids].ssid_len =
  1242. ssid->ssid_len;
  1243. params.num_ssids++;
  1244. if (params.num_ssids >= max_sched_scan_ssids) {
  1245. wpa_s->prev_sched_ssid = ssid;
  1246. do {
  1247. ssid = ssid->next;
  1248. } while (ssid &&
  1249. (wpas_network_disabled(wpa_s, ssid) ||
  1250. !ssid->scan_ssid));
  1251. break;
  1252. }
  1253. }
  1254. next:
  1255. wpa_s->prev_sched_ssid = ssid;
  1256. ssid = ssid->next;
  1257. }
  1258. if (params.num_filter_ssids == 0) {
  1259. os_free(params.filter_ssids);
  1260. params.filter_ssids = NULL;
  1261. }
  1262. extra_ie = wpa_supplicant_extra_ies(wpa_s);
  1263. if (extra_ie) {
  1264. params.extra_ies = wpabuf_head(extra_ie);
  1265. params.extra_ies_len = wpabuf_len(extra_ie);
  1266. }
  1267. if (wpa_s->conf->filter_rssi)
  1268. params.filter_rssi = wpa_s->conf->filter_rssi;
  1269. /* See if user specified frequencies. If so, scan only those. */
  1270. if (wpa_s->conf->freq_list && !params.freqs) {
  1271. wpa_dbg(wpa_s, MSG_DEBUG,
  1272. "Optimize scan based on conf->freq_list");
  1273. int_array_concat(&params.freqs, wpa_s->conf->freq_list);
  1274. }
  1275. scan_params = &params;
  1276. scan:
  1277. wpa_s->sched_scan_timed_out = 0;
  1278. /*
  1279. * We cannot support multiple scan plans if the scan request includes
  1280. * too many SSID's, so in this case use only the last scan plan and make
  1281. * it run infinitely. It will be stopped by the timeout.
  1282. */
  1283. if (wpa_s->sched_scan_plans_num == 1 ||
  1284. (wpa_s->sched_scan_plans_num && !ssid && wpa_s->first_sched_scan)) {
  1285. params.sched_scan_plans = wpa_s->sched_scan_plans;
  1286. params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
  1287. } else if (wpa_s->sched_scan_plans_num > 1) {
  1288. wpa_dbg(wpa_s, MSG_DEBUG,
  1289. "Too many SSIDs. Default to using single scheduled_scan plan");
  1290. params.sched_scan_plans =
  1291. &wpa_s->sched_scan_plans[wpa_s->sched_scan_plans_num -
  1292. 1];
  1293. params.sched_scan_plans_num = 1;
  1294. } else {
  1295. if (wpa_s->conf->sched_scan_interval)
  1296. scan_plan.interval = wpa_s->conf->sched_scan_interval;
  1297. else
  1298. scan_plan.interval = 10;
  1299. if (scan_plan.interval > wpa_s->max_sched_scan_plan_interval) {
  1300. wpa_printf(MSG_WARNING,
  1301. "Scan interval too long(%u), use the maximum allowed(%u)",
  1302. scan_plan.interval,
  1303. wpa_s->max_sched_scan_plan_interval);
  1304. scan_plan.interval =
  1305. wpa_s->max_sched_scan_plan_interval;
  1306. }
  1307. scan_plan.iterations = 0;
  1308. params.sched_scan_plans = &scan_plan;
  1309. params.sched_scan_plans_num = 1;
  1310. }
  1311. params.sched_scan_start_delay = wpa_s->conf->sched_scan_start_delay;
  1312. if (ssid || !wpa_s->first_sched_scan) {
  1313. wpa_dbg(wpa_s, MSG_DEBUG,
  1314. "Starting sched scan after %u seconds: interval %u timeout %d",
  1315. params.sched_scan_start_delay,
  1316. params.sched_scan_plans[0].interval,
  1317. wpa_s->sched_scan_timeout);
  1318. } else {
  1319. wpa_dbg(wpa_s, MSG_DEBUG,
  1320. "Starting sched scan after %u seconds (no timeout)",
  1321. params.sched_scan_start_delay);
  1322. }
  1323. wpa_setband_scan_freqs(wpa_s, scan_params);
  1324. if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCHED_SCAN) &&
  1325. wpa_s->wpa_state <= WPA_SCANNING) {
  1326. params.mac_addr_rand = 1;
  1327. if (wpa_s->mac_addr_sched_scan) {
  1328. params.mac_addr = wpa_s->mac_addr_sched_scan;
  1329. params.mac_addr_mask = wpa_s->mac_addr_sched_scan +
  1330. ETH_ALEN;
  1331. }
  1332. }
  1333. wpa_scan_set_relative_rssi_params(wpa_s, scan_params);
  1334. ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params);
  1335. wpabuf_free(extra_ie);
  1336. os_free(params.filter_ssids);
  1337. if (ret) {
  1338. wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
  1339. if (prev_state != wpa_s->wpa_state)
  1340. wpa_supplicant_set_state(wpa_s, prev_state);
  1341. return ret;
  1342. }
  1343. /* If we have more SSIDs to scan, add a timeout so we scan them too */
  1344. if (ssid || !wpa_s->first_sched_scan) {
  1345. wpa_s->sched_scan_timed_out = 0;
  1346. eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
  1347. wpa_supplicant_sched_scan_timeout,
  1348. wpa_s, NULL);
  1349. wpa_s->first_sched_scan = 0;
  1350. wpa_s->sched_scan_timeout /= 2;
  1351. params.sched_scan_plans[0].interval *= 2;
  1352. if ((unsigned int) wpa_s->sched_scan_timeout <
  1353. params.sched_scan_plans[0].interval ||
  1354. params.sched_scan_plans[0].interval >
  1355. wpa_s->max_sched_scan_plan_interval) {
  1356. params.sched_scan_plans[0].interval = 10;
  1357. wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
  1358. }
  1359. }
  1360. /* If there is no more ssids, start next time from the beginning */
  1361. if (!ssid)
  1362. wpa_s->prev_sched_ssid = NULL;
  1363. return 0;
  1364. }
  1365. /**
  1366. * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
  1367. * @wpa_s: Pointer to wpa_supplicant data
  1368. *
  1369. * This function is used to cancel a scan request scheduled with
  1370. * wpa_supplicant_req_scan().
  1371. */
  1372. void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
  1373. {
  1374. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
  1375. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  1376. }
  1377. /**
  1378. * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
  1379. * @wpa_s: Pointer to wpa_supplicant data
  1380. *
  1381. * This function is used to stop a delayed scheduled scan.
  1382. */
  1383. void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
  1384. {
  1385. if (!wpa_s->sched_scan_supported)
  1386. return;
  1387. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
  1388. eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
  1389. wpa_s, NULL);
  1390. }
  1391. /**
  1392. * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
  1393. * @wpa_s: Pointer to wpa_supplicant data
  1394. *
  1395. * This function is used to stop a periodic scheduled scan.
  1396. */
  1397. void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
  1398. {
  1399. if (!wpa_s->sched_scanning)
  1400. return;
  1401. if (wpa_s->sched_scanning)
  1402. wpa_s->sched_scan_stop_req = 1;
  1403. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
  1404. eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
  1405. wpa_supplicant_stop_sched_scan(wpa_s);
  1406. }
  1407. /**
  1408. * wpa_supplicant_notify_scanning - Indicate possible scan state change
  1409. * @wpa_s: Pointer to wpa_supplicant data
  1410. * @scanning: Whether scanning is currently in progress
  1411. *
  1412. * This function is to generate scanning notifycations. It is called whenever
  1413. * there may have been a change in scanning (scan started, completed, stopped).
  1414. * wpas_notify_scanning() is called whenever the scanning state changed from the
  1415. * previously notified state.
  1416. */
  1417. void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
  1418. int scanning)
  1419. {
  1420. if (wpa_s->scanning != scanning) {
  1421. wpa_s->scanning = scanning;
  1422. wpas_notify_scanning(wpa_s);
  1423. }
  1424. }
  1425. static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
  1426. {
  1427. int rate = 0;
  1428. const u8 *ie;
  1429. int i;
  1430. ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
  1431. for (i = 0; ie && i < ie[1]; i++) {
  1432. if ((ie[i + 2] & 0x7f) > rate)
  1433. rate = ie[i + 2] & 0x7f;
  1434. }
  1435. ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
  1436. for (i = 0; ie && i < ie[1]; i++) {
  1437. if ((ie[i + 2] & 0x7f) > rate)
  1438. rate = ie[i + 2] & 0x7f;
  1439. }
  1440. return rate;
  1441. }
  1442. /**
  1443. * wpa_scan_get_ie - Fetch a specified information element from a scan result
  1444. * @res: Scan result entry
  1445. * @ie: Information element identitifier (WLAN_EID_*)
  1446. * Returns: Pointer to the information element (id field) or %NULL if not found
  1447. *
  1448. * This function returns the first matching information element in the scan
  1449. * result.
  1450. */
  1451. const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
  1452. {
  1453. size_t ie_len = res->ie_len;
  1454. /* Use the Beacon frame IEs if res->ie_len is not available */
  1455. if (!ie_len)
  1456. ie_len = res->beacon_ie_len;
  1457. return get_ie((const u8 *) (res + 1), ie_len, ie);
  1458. }
  1459. /**
  1460. * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
  1461. * @res: Scan result entry
  1462. * @vendor_type: Vendor type (four octets starting the IE payload)
  1463. * Returns: Pointer to the information element (id field) or %NULL if not found
  1464. *
  1465. * This function returns the first matching information element in the scan
  1466. * result.
  1467. */
  1468. const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
  1469. u32 vendor_type)
  1470. {
  1471. const u8 *end, *pos;
  1472. pos = (const u8 *) (res + 1);
  1473. end = pos + res->ie_len;
  1474. while (end - pos > 1) {
  1475. if (2 + pos[1] > end - pos)
  1476. break;
  1477. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  1478. vendor_type == WPA_GET_BE32(&pos[2]))
  1479. return pos;
  1480. pos += 2 + pos[1];
  1481. }
  1482. return NULL;
  1483. }
  1484. /**
  1485. * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
  1486. * @res: Scan result entry
  1487. * @vendor_type: Vendor type (four octets starting the IE payload)
  1488. * Returns: Pointer to the information element (id field) or %NULL if not found
  1489. *
  1490. * This function returns the first matching information element in the scan
  1491. * result.
  1492. *
  1493. * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
  1494. * from Beacon frames instead of either Beacon or Probe Response frames.
  1495. */
  1496. const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
  1497. u32 vendor_type)
  1498. {
  1499. const u8 *end, *pos;
  1500. if (res->beacon_ie_len == 0)
  1501. return NULL;
  1502. pos = (const u8 *) (res + 1);
  1503. pos += res->ie_len;
  1504. end = pos + res->beacon_ie_len;
  1505. while (end - pos > 1) {
  1506. if (2 + pos[1] > end - pos)
  1507. break;
  1508. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  1509. vendor_type == WPA_GET_BE32(&pos[2]))
  1510. return pos;
  1511. pos += 2 + pos[1];
  1512. }
  1513. return NULL;
  1514. }
  1515. /**
  1516. * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
  1517. * @res: Scan result entry
  1518. * @vendor_type: Vendor type (four octets starting the IE payload)
  1519. * Returns: Pointer to the information element payload or %NULL if not found
  1520. *
  1521. * This function returns concatenated payload of possibly fragmented vendor
  1522. * specific information elements in the scan result. The caller is responsible
  1523. * for freeing the returned buffer.
  1524. */
  1525. struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
  1526. u32 vendor_type)
  1527. {
  1528. struct wpabuf *buf;
  1529. const u8 *end, *pos;
  1530. buf = wpabuf_alloc(res->ie_len);
  1531. if (buf == NULL)
  1532. return NULL;
  1533. pos = (const u8 *) (res + 1);
  1534. end = pos + res->ie_len;
  1535. while (end - pos > 1) {
  1536. if (2 + pos[1] > end - pos)
  1537. break;
  1538. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  1539. vendor_type == WPA_GET_BE32(&pos[2]))
  1540. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  1541. pos += 2 + pos[1];
  1542. }
  1543. if (wpabuf_len(buf) == 0) {
  1544. wpabuf_free(buf);
  1545. buf = NULL;
  1546. }
  1547. return buf;
  1548. }
  1549. /*
  1550. * Channels with a great SNR can operate at full rate. What is a great SNR?
  1551. * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
  1552. * rule of thumb is that any SNR above 20 is good." This one
  1553. * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
  1554. * recommends 25 as a minimum SNR for 54 Mbps data rate. The estimates used in
  1555. * scan_est_throughput() allow even smaller SNR values for the maximum rates
  1556. * (21 for 54 Mbps, 22 for VHT80 MCS9, 24 for HT40 and HT20 MCS7). Use 25 as a
  1557. * somewhat conservative value here.
  1558. */
  1559. #define GREAT_SNR 25
  1560. #define IS_5GHZ(n) (n > 4000)
  1561. /* Compare function for sorting scan results. Return >0 if @b is considered
  1562. * better. */
  1563. static int wpa_scan_result_compar(const void *a, const void *b)
  1564. {
  1565. #define MIN(a,b) a < b ? a : b
  1566. struct wpa_scan_res **_wa = (void *) a;
  1567. struct wpa_scan_res **_wb = (void *) b;
  1568. struct wpa_scan_res *wa = *_wa;
  1569. struct wpa_scan_res *wb = *_wb;
  1570. int wpa_a, wpa_b;
  1571. int snr_a, snr_b, snr_a_full, snr_b_full;
  1572. /* WPA/WPA2 support preferred */
  1573. wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
  1574. wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
  1575. wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
  1576. wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
  1577. if (wpa_b && !wpa_a)
  1578. return 1;
  1579. if (!wpa_b && wpa_a)
  1580. return -1;
  1581. /* privacy support preferred */
  1582. if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
  1583. (wb->caps & IEEE80211_CAP_PRIVACY))
  1584. return 1;
  1585. if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
  1586. (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
  1587. return -1;
  1588. if (wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) {
  1589. snr_a_full = wa->snr;
  1590. snr_a = MIN(wa->snr, GREAT_SNR);
  1591. snr_b_full = wb->snr;
  1592. snr_b = MIN(wb->snr, GREAT_SNR);
  1593. } else {
  1594. /* Level is not in dBm, so we can't calculate
  1595. * SNR. Just use raw level (units unknown). */
  1596. snr_a = snr_a_full = wa->level;
  1597. snr_b = snr_b_full = wb->level;
  1598. }
  1599. /* if SNR is close, decide by max rate or frequency band */
  1600. if (snr_a && snr_b && abs(snr_b - snr_a) < 7) {
  1601. if (wa->est_throughput != wb->est_throughput)
  1602. return wb->est_throughput - wa->est_throughput;
  1603. }
  1604. if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
  1605. (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
  1606. if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
  1607. return IS_5GHZ(wa->freq) ? -1 : 1;
  1608. }
  1609. /* all things being equal, use SNR; if SNRs are
  1610. * identical, use quality values since some drivers may only report
  1611. * that value and leave the signal level zero */
  1612. if (snr_b_full == snr_a_full)
  1613. return wb->qual - wa->qual;
  1614. return snr_b_full - snr_a_full;
  1615. #undef MIN
  1616. }
  1617. #ifdef CONFIG_WPS
  1618. /* Compare function for sorting scan results when searching a WPS AP for
  1619. * provisioning. Return >0 if @b is considered better. */
  1620. static int wpa_scan_result_wps_compar(const void *a, const void *b)
  1621. {
  1622. struct wpa_scan_res **_wa = (void *) a;
  1623. struct wpa_scan_res **_wb = (void *) b;
  1624. struct wpa_scan_res *wa = *_wa;
  1625. struct wpa_scan_res *wb = *_wb;
  1626. int uses_wps_a, uses_wps_b;
  1627. struct wpabuf *wps_a, *wps_b;
  1628. int res;
  1629. /* Optimization - check WPS IE existence before allocated memory and
  1630. * doing full reassembly. */
  1631. uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
  1632. uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
  1633. if (uses_wps_a && !uses_wps_b)
  1634. return -1;
  1635. if (!uses_wps_a && uses_wps_b)
  1636. return 1;
  1637. if (uses_wps_a && uses_wps_b) {
  1638. wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
  1639. wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
  1640. res = wps_ap_priority_compar(wps_a, wps_b);
  1641. wpabuf_free(wps_a);
  1642. wpabuf_free(wps_b);
  1643. if (res)
  1644. return res;
  1645. }
  1646. /*
  1647. * Do not use current AP security policy as a sorting criteria during
  1648. * WPS provisioning step since the AP may get reconfigured at the
  1649. * completion of provisioning.
  1650. */
  1651. /* all things being equal, use signal level; if signal levels are
  1652. * identical, use quality values since some drivers may only report
  1653. * that value and leave the signal level zero */
  1654. if (wb->level == wa->level)
  1655. return wb->qual - wa->qual;
  1656. return wb->level - wa->level;
  1657. }
  1658. #endif /* CONFIG_WPS */
  1659. static void dump_scan_res(struct wpa_scan_results *scan_res)
  1660. {
  1661. #ifndef CONFIG_NO_STDOUT_DEBUG
  1662. size_t i;
  1663. if (scan_res->res == NULL || scan_res->num == 0)
  1664. return;
  1665. wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
  1666. for (i = 0; i < scan_res->num; i++) {
  1667. struct wpa_scan_res *r = scan_res->res[i];
  1668. u8 *pos;
  1669. if (r->flags & WPA_SCAN_LEVEL_DBM) {
  1670. int noise_valid = !(r->flags & WPA_SCAN_NOISE_INVALID);
  1671. wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
  1672. "noise=%d%s level=%d snr=%d%s flags=0x%x age=%u est=%u",
  1673. MAC2STR(r->bssid), r->freq, r->qual,
  1674. r->noise, noise_valid ? "" : "~", r->level,
  1675. r->snr, r->snr >= GREAT_SNR ? "*" : "",
  1676. r->flags,
  1677. r->age, r->est_throughput);
  1678. } else {
  1679. wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
  1680. "noise=%d level=%d flags=0x%x age=%u est=%u",
  1681. MAC2STR(r->bssid), r->freq, r->qual,
  1682. r->noise, r->level, r->flags, r->age,
  1683. r->est_throughput);
  1684. }
  1685. pos = (u8 *) (r + 1);
  1686. if (r->ie_len)
  1687. wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
  1688. pos += r->ie_len;
  1689. if (r->beacon_ie_len)
  1690. wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
  1691. pos, r->beacon_ie_len);
  1692. }
  1693. #endif /* CONFIG_NO_STDOUT_DEBUG */
  1694. }
  1695. /**
  1696. * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
  1697. * @wpa_s: Pointer to wpa_supplicant data
  1698. * @bssid: BSSID to check
  1699. * Returns: 0 if the BSSID is filtered or 1 if not
  1700. *
  1701. * This function is used to filter out specific BSSIDs from scan reslts mainly
  1702. * for testing purposes (SET bssid_filter ctrl_iface command).
  1703. */
  1704. int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
  1705. const u8 *bssid)
  1706. {
  1707. size_t i;
  1708. if (wpa_s->bssid_filter == NULL)
  1709. return 1;
  1710. for (i = 0; i < wpa_s->bssid_filter_count; i++) {
  1711. if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
  1712. ETH_ALEN) == 0)
  1713. return 1;
  1714. }
  1715. return 0;
  1716. }
  1717. void filter_scan_res(struct wpa_supplicant *wpa_s,
  1718. struct wpa_scan_results *res)
  1719. {
  1720. size_t i, j;
  1721. if (wpa_s->bssid_filter == NULL)
  1722. return;
  1723. for (i = 0, j = 0; i < res->num; i++) {
  1724. if (wpa_supplicant_filter_bssid_match(wpa_s,
  1725. res->res[i]->bssid)) {
  1726. res->res[j++] = res->res[i];
  1727. } else {
  1728. os_free(res->res[i]);
  1729. res->res[i] = NULL;
  1730. }
  1731. }
  1732. if (res->num != j) {
  1733. wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
  1734. (int) (res->num - j));
  1735. res->num = j;
  1736. }
  1737. }
  1738. /*
  1739. * Noise floor values to use when we have signal strength
  1740. * measurements, but no noise floor measurements. These values were
  1741. * measured in an office environment with many APs.
  1742. */
  1743. #define DEFAULT_NOISE_FLOOR_2GHZ (-89)
  1744. #define DEFAULT_NOISE_FLOOR_5GHZ (-92)
  1745. void scan_snr(struct wpa_scan_res *res)
  1746. {
  1747. if (res->flags & WPA_SCAN_NOISE_INVALID) {
  1748. res->noise = IS_5GHZ(res->freq) ?
  1749. DEFAULT_NOISE_FLOOR_5GHZ :
  1750. DEFAULT_NOISE_FLOOR_2GHZ;
  1751. }
  1752. if (res->flags & WPA_SCAN_LEVEL_DBM) {
  1753. res->snr = res->level - res->noise;
  1754. } else {
  1755. /* Level is not in dBm, so we can't calculate
  1756. * SNR. Just use raw level (units unknown). */
  1757. res->snr = res->level;
  1758. }
  1759. }
  1760. static unsigned int max_ht20_rate(int snr)
  1761. {
  1762. if (snr < 6)
  1763. return 6500; /* HT20 MCS0 */
  1764. if (snr < 8)
  1765. return 13000; /* HT20 MCS1 */
  1766. if (snr < 13)
  1767. return 19500; /* HT20 MCS2 */
  1768. if (snr < 17)
  1769. return 26000; /* HT20 MCS3 */
  1770. if (snr < 20)
  1771. return 39000; /* HT20 MCS4 */
  1772. if (snr < 23)
  1773. return 52000; /* HT20 MCS5 */
  1774. if (snr < 24)
  1775. return 58500; /* HT20 MCS6 */
  1776. return 65000; /* HT20 MCS7 */
  1777. }
  1778. static unsigned int max_ht40_rate(int snr)
  1779. {
  1780. if (snr < 3)
  1781. return 13500; /* HT40 MCS0 */
  1782. if (snr < 6)
  1783. return 27000; /* HT40 MCS1 */
  1784. if (snr < 10)
  1785. return 40500; /* HT40 MCS2 */
  1786. if (snr < 15)
  1787. return 54000; /* HT40 MCS3 */
  1788. if (snr < 17)
  1789. return 81000; /* HT40 MCS4 */
  1790. if (snr < 22)
  1791. return 108000; /* HT40 MCS5 */
  1792. if (snr < 24)
  1793. return 121500; /* HT40 MCS6 */
  1794. return 135000; /* HT40 MCS7 */
  1795. }
  1796. static unsigned int max_vht80_rate(int snr)
  1797. {
  1798. if (snr < 1)
  1799. return 0;
  1800. if (snr < 2)
  1801. return 29300; /* VHT80 MCS0 */
  1802. if (snr < 5)
  1803. return 58500; /* VHT80 MCS1 */
  1804. if (snr < 9)
  1805. return 87800; /* VHT80 MCS2 */
  1806. if (snr < 11)
  1807. return 117000; /* VHT80 MCS3 */
  1808. if (snr < 15)
  1809. return 175500; /* VHT80 MCS4 */
  1810. if (snr < 16)
  1811. return 234000; /* VHT80 MCS5 */
  1812. if (snr < 18)
  1813. return 263300; /* VHT80 MCS6 */
  1814. if (snr < 20)
  1815. return 292500; /* VHT80 MCS7 */
  1816. if (snr < 22)
  1817. return 351000; /* VHT80 MCS8 */
  1818. return 390000; /* VHT80 MCS9 */
  1819. }
  1820. void scan_est_throughput(struct wpa_supplicant *wpa_s,
  1821. struct wpa_scan_res *res)
  1822. {
  1823. enum local_hw_capab capab = wpa_s->hw_capab;
  1824. int rate; /* max legacy rate in 500 kb/s units */
  1825. const u8 *ie;
  1826. unsigned int est, tmp;
  1827. int snr = res->snr;
  1828. if (res->est_throughput)
  1829. return;
  1830. /* Get maximum legacy rate */
  1831. rate = wpa_scan_get_max_rate(res);
  1832. /* Limit based on estimated SNR */
  1833. if (rate > 1 * 2 && snr < 1)
  1834. rate = 1 * 2;
  1835. else if (rate > 2 * 2 && snr < 4)
  1836. rate = 2 * 2;
  1837. else if (rate > 6 * 2 && snr < 5)
  1838. rate = 6 * 2;
  1839. else if (rate > 9 * 2 && snr < 6)
  1840. rate = 9 * 2;
  1841. else if (rate > 12 * 2 && snr < 7)
  1842. rate = 12 * 2;
  1843. else if (rate > 18 * 2 && snr < 10)
  1844. rate = 18 * 2;
  1845. else if (rate > 24 * 2 && snr < 11)
  1846. rate = 24 * 2;
  1847. else if (rate > 36 * 2 && snr < 15)
  1848. rate = 36 * 2;
  1849. else if (rate > 48 * 2 && snr < 19)
  1850. rate = 48 * 2;
  1851. else if (rate > 54 * 2 && snr < 21)
  1852. rate = 54 * 2;
  1853. est = rate * 500;
  1854. if (capab == CAPAB_HT || capab == CAPAB_HT40 || capab == CAPAB_VHT) {
  1855. ie = wpa_scan_get_ie(res, WLAN_EID_HT_CAP);
  1856. if (ie) {
  1857. tmp = max_ht20_rate(snr);
  1858. if (tmp > est)
  1859. est = tmp;
  1860. }
  1861. }
  1862. if (capab == CAPAB_HT40 || capab == CAPAB_VHT) {
  1863. ie = wpa_scan_get_ie(res, WLAN_EID_HT_OPERATION);
  1864. if (ie && ie[1] >= 2 &&
  1865. (ie[3] & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
  1866. tmp = max_ht40_rate(snr);
  1867. if (tmp > est)
  1868. est = tmp;
  1869. }
  1870. }
  1871. if (capab == CAPAB_VHT) {
  1872. /* Use +1 to assume VHT is always faster than HT */
  1873. ie = wpa_scan_get_ie(res, WLAN_EID_VHT_CAP);
  1874. if (ie) {
  1875. tmp = max_ht20_rate(snr) + 1;
  1876. if (tmp > est)
  1877. est = tmp;
  1878. ie = wpa_scan_get_ie(res, WLAN_EID_HT_OPERATION);
  1879. if (ie && ie[1] >= 2 &&
  1880. (ie[3] &
  1881. HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
  1882. tmp = max_ht40_rate(snr) + 1;
  1883. if (tmp > est)
  1884. est = tmp;
  1885. }
  1886. ie = wpa_scan_get_ie(res, WLAN_EID_VHT_OPERATION);
  1887. if (ie && ie[1] >= 1 &&
  1888. (ie[2] & VHT_OPMODE_CHANNEL_WIDTH_MASK)) {
  1889. tmp = max_vht80_rate(snr) + 1;
  1890. if (tmp > est)
  1891. est = tmp;
  1892. }
  1893. }
  1894. }
  1895. /* TODO: channel utilization and AP load (e.g., from AP Beacon) */
  1896. res->est_throughput = est;
  1897. }
  1898. /**
  1899. * wpa_supplicant_get_scan_results - Get scan results
  1900. * @wpa_s: Pointer to wpa_supplicant data
  1901. * @info: Information about what was scanned or %NULL if not available
  1902. * @new_scan: Whether a new scan was performed
  1903. * Returns: Scan results, %NULL on failure
  1904. *
  1905. * This function request the current scan results from the driver and updates
  1906. * the local BSS list wpa_s->bss. The caller is responsible for freeing the
  1907. * results with wpa_scan_results_free().
  1908. */
  1909. struct wpa_scan_results *
  1910. wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
  1911. struct scan_info *info, int new_scan)
  1912. {
  1913. struct wpa_scan_results *scan_res;
  1914. size_t i;
  1915. int (*compar)(const void *, const void *) = wpa_scan_result_compar;
  1916. scan_res = wpa_drv_get_scan_results2(wpa_s);
  1917. if (scan_res == NULL) {
  1918. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
  1919. return NULL;
  1920. }
  1921. if (scan_res->fetch_time.sec == 0) {
  1922. /*
  1923. * Make sure we have a valid timestamp if the driver wrapper
  1924. * does not set this.
  1925. */
  1926. os_get_reltime(&scan_res->fetch_time);
  1927. }
  1928. filter_scan_res(wpa_s, scan_res);
  1929. for (i = 0; i < scan_res->num; i++) {
  1930. struct wpa_scan_res *scan_res_item = scan_res->res[i];
  1931. scan_snr(scan_res_item);
  1932. scan_est_throughput(wpa_s, scan_res_item);
  1933. }
  1934. #ifdef CONFIG_WPS
  1935. if (wpas_wps_searching(wpa_s)) {
  1936. wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
  1937. "provisioning rules");
  1938. compar = wpa_scan_result_wps_compar;
  1939. }
  1940. #endif /* CONFIG_WPS */
  1941. if (scan_res->res) {
  1942. qsort(scan_res->res, scan_res->num,
  1943. sizeof(struct wpa_scan_res *), compar);
  1944. }
  1945. dump_scan_res(scan_res);
  1946. if (wpa_s->ignore_post_flush_scan_res) {
  1947. /* FLUSH command aborted an ongoing scan and these are the
  1948. * results from the aborted scan. Do not process the results to
  1949. * maintain flushed state. */
  1950. wpa_dbg(wpa_s, MSG_DEBUG,
  1951. "Do not update BSS table based on pending post-FLUSH scan results");
  1952. wpa_s->ignore_post_flush_scan_res = 0;
  1953. return scan_res;
  1954. }
  1955. wpa_bss_update_start(wpa_s);
  1956. for (i = 0; i < scan_res->num; i++)
  1957. wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
  1958. &scan_res->fetch_time);
  1959. wpa_bss_update_end(wpa_s, info, new_scan);
  1960. return scan_res;
  1961. }
  1962. /**
  1963. * wpa_supplicant_update_scan_results - Update scan results from the driver
  1964. * @wpa_s: Pointer to wpa_supplicant data
  1965. * Returns: 0 on success, -1 on failure
  1966. *
  1967. * This function updates the BSS table within wpa_supplicant based on the
  1968. * currently available scan results from the driver without requesting a new
  1969. * scan. This is used in cases where the driver indicates an association
  1970. * (including roaming within ESS) and wpa_supplicant does not yet have the
  1971. * needed information to complete the connection (e.g., to perform validation
  1972. * steps in 4-way handshake).
  1973. */
  1974. int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
  1975. {
  1976. struct wpa_scan_results *scan_res;
  1977. scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
  1978. if (scan_res == NULL)
  1979. return -1;
  1980. wpa_scan_results_free(scan_res);
  1981. return 0;
  1982. }
  1983. /**
  1984. * scan_only_handler - Reports scan results
  1985. */
  1986. void scan_only_handler(struct wpa_supplicant *wpa_s,
  1987. struct wpa_scan_results *scan_res)
  1988. {
  1989. wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
  1990. if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
  1991. wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
  1992. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
  1993. wpa_s->manual_scan_id);
  1994. wpa_s->manual_scan_use_id = 0;
  1995. } else {
  1996. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
  1997. }
  1998. wpas_notify_scan_results(wpa_s);
  1999. wpas_notify_scan_done(wpa_s, 1);
  2000. if (wpa_s->scan_work) {
  2001. struct wpa_radio_work *work = wpa_s->scan_work;
  2002. wpa_s->scan_work = NULL;
  2003. radio_work_done(work);
  2004. }
  2005. if (wpa_s->wpa_state == WPA_SCANNING)
  2006. wpa_supplicant_set_state(wpa_s, wpa_s->scan_prev_wpa_state);
  2007. }
  2008. int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
  2009. {
  2010. return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
  2011. }
  2012. struct wpa_driver_scan_params *
  2013. wpa_scan_clone_params(const struct wpa_driver_scan_params *src)
  2014. {
  2015. struct wpa_driver_scan_params *params;
  2016. size_t i;
  2017. u8 *n;
  2018. params = os_zalloc(sizeof(*params));
  2019. if (params == NULL)
  2020. return NULL;
  2021. for (i = 0; i < src->num_ssids; i++) {
  2022. if (src->ssids[i].ssid) {
  2023. n = os_memdup(src->ssids[i].ssid,
  2024. src->ssids[i].ssid_len);
  2025. if (n == NULL)
  2026. goto failed;
  2027. params->ssids[i].ssid = n;
  2028. params->ssids[i].ssid_len = src->ssids[i].ssid_len;
  2029. }
  2030. }
  2031. params->num_ssids = src->num_ssids;
  2032. if (src->extra_ies) {
  2033. n = os_memdup(src->extra_ies, src->extra_ies_len);
  2034. if (n == NULL)
  2035. goto failed;
  2036. params->extra_ies = n;
  2037. params->extra_ies_len = src->extra_ies_len;
  2038. }
  2039. if (src->freqs) {
  2040. int len = int_array_len(src->freqs);
  2041. params->freqs = os_memdup(src->freqs, (len + 1) * sizeof(int));
  2042. if (params->freqs == NULL)
  2043. goto failed;
  2044. }
  2045. if (src->filter_ssids) {
  2046. params->filter_ssids = os_memdup(src->filter_ssids,
  2047. sizeof(*params->filter_ssids) *
  2048. src->num_filter_ssids);
  2049. if (params->filter_ssids == NULL)
  2050. goto failed;
  2051. params->num_filter_ssids = src->num_filter_ssids;
  2052. }
  2053. params->filter_rssi = src->filter_rssi;
  2054. params->p2p_probe = src->p2p_probe;
  2055. params->only_new_results = src->only_new_results;
  2056. params->low_priority = src->low_priority;
  2057. params->duration = src->duration;
  2058. params->duration_mandatory = src->duration_mandatory;
  2059. if (src->sched_scan_plans_num > 0) {
  2060. params->sched_scan_plans =
  2061. os_memdup(src->sched_scan_plans,
  2062. sizeof(*src->sched_scan_plans) *
  2063. src->sched_scan_plans_num);
  2064. if (!params->sched_scan_plans)
  2065. goto failed;
  2066. params->sched_scan_plans_num = src->sched_scan_plans_num;
  2067. }
  2068. if (src->mac_addr_rand) {
  2069. params->mac_addr_rand = src->mac_addr_rand;
  2070. if (src->mac_addr && src->mac_addr_mask) {
  2071. u8 *mac_addr;
  2072. mac_addr = os_malloc(2 * ETH_ALEN);
  2073. if (!mac_addr)
  2074. goto failed;
  2075. os_memcpy(mac_addr, src->mac_addr, ETH_ALEN);
  2076. os_memcpy(mac_addr + ETH_ALEN, src->mac_addr_mask,
  2077. ETH_ALEN);
  2078. params->mac_addr = mac_addr;
  2079. params->mac_addr_mask = mac_addr + ETH_ALEN;
  2080. }
  2081. }
  2082. if (src->bssid) {
  2083. u8 *bssid;
  2084. bssid = os_memdup(src->bssid, ETH_ALEN);
  2085. if (!bssid)
  2086. goto failed;
  2087. params->bssid = bssid;
  2088. }
  2089. params->relative_rssi_set = src->relative_rssi_set;
  2090. params->relative_rssi = src->relative_rssi;
  2091. params->relative_adjust_band = src->relative_adjust_band;
  2092. params->relative_adjust_rssi = src->relative_adjust_rssi;
  2093. return params;
  2094. failed:
  2095. wpa_scan_free_params(params);
  2096. return NULL;
  2097. }
  2098. void wpa_scan_free_params(struct wpa_driver_scan_params *params)
  2099. {
  2100. size_t i;
  2101. if (params == NULL)
  2102. return;
  2103. for (i = 0; i < params->num_ssids; i++)
  2104. os_free((u8 *) params->ssids[i].ssid);
  2105. os_free((u8 *) params->extra_ies);
  2106. os_free(params->freqs);
  2107. os_free(params->filter_ssids);
  2108. os_free(params->sched_scan_plans);
  2109. /*
  2110. * Note: params->mac_addr_mask points to same memory allocation and
  2111. * must not be freed separately.
  2112. */
  2113. os_free((u8 *) params->mac_addr);
  2114. os_free((u8 *) params->bssid);
  2115. os_free(params);
  2116. }
  2117. int wpas_start_pno(struct wpa_supplicant *wpa_s)
  2118. {
  2119. int ret, prio;
  2120. size_t i, num_ssid, num_match_ssid;
  2121. struct wpa_ssid *ssid;
  2122. struct wpa_driver_scan_params params;
  2123. struct sched_scan_plan scan_plan;
  2124. unsigned int max_sched_scan_ssids;
  2125. if (!wpa_s->sched_scan_supported)
  2126. return -1;
  2127. if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
  2128. max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
  2129. else
  2130. max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
  2131. if (max_sched_scan_ssids < 1)
  2132. return -1;
  2133. if (wpa_s->pno || wpa_s->pno_sched_pending)
  2134. return 0;
  2135. if ((wpa_s->wpa_state > WPA_SCANNING) &&
  2136. (wpa_s->wpa_state < WPA_COMPLETED)) {
  2137. wpa_printf(MSG_ERROR, "PNO: In assoc process");
  2138. return -EAGAIN;
  2139. }
  2140. if (wpa_s->wpa_state == WPA_SCANNING) {
  2141. wpa_supplicant_cancel_scan(wpa_s);
  2142. if (wpa_s->sched_scanning) {
  2143. wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
  2144. "ongoing sched scan");
  2145. wpa_supplicant_cancel_sched_scan(wpa_s);
  2146. wpa_s->pno_sched_pending = 1;
  2147. return 0;
  2148. }
  2149. }
  2150. if (wpa_s->sched_scan_stop_req) {
  2151. wpa_printf(MSG_DEBUG,
  2152. "Schedule PNO after previous sched scan has stopped");
  2153. wpa_s->pno_sched_pending = 1;
  2154. return 0;
  2155. }
  2156. os_memset(&params, 0, sizeof(params));
  2157. num_ssid = num_match_ssid = 0;
  2158. ssid = wpa_s->conf->ssid;
  2159. while (ssid) {
  2160. if (!wpas_network_disabled(wpa_s, ssid)) {
  2161. num_match_ssid++;
  2162. if (ssid->scan_ssid)
  2163. num_ssid++;
  2164. }
  2165. ssid = ssid->next;
  2166. }
  2167. if (num_match_ssid == 0) {
  2168. wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
  2169. return -1;
  2170. }
  2171. if (num_match_ssid > num_ssid) {
  2172. params.num_ssids++; /* wildcard */
  2173. num_ssid++;
  2174. }
  2175. if (num_ssid > max_sched_scan_ssids) {
  2176. wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
  2177. "%u", max_sched_scan_ssids, (unsigned int) num_ssid);
  2178. num_ssid = max_sched_scan_ssids;
  2179. }
  2180. if (num_match_ssid > wpa_s->max_match_sets) {
  2181. num_match_ssid = wpa_s->max_match_sets;
  2182. wpa_dbg(wpa_s, MSG_DEBUG, "PNO: Too many SSIDs to match");
  2183. }
  2184. params.filter_ssids = os_calloc(num_match_ssid,
  2185. sizeof(struct wpa_driver_scan_filter));
  2186. if (params.filter_ssids == NULL)
  2187. return -1;
  2188. i = 0;
  2189. prio = 0;
  2190. ssid = wpa_s->conf->pssid[prio];
  2191. while (ssid) {
  2192. if (!wpas_network_disabled(wpa_s, ssid)) {
  2193. if (ssid->scan_ssid && params.num_ssids < num_ssid) {
  2194. params.ssids[params.num_ssids].ssid =
  2195. ssid->ssid;
  2196. params.ssids[params.num_ssids].ssid_len =
  2197. ssid->ssid_len;
  2198. params.num_ssids++;
  2199. }
  2200. os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
  2201. ssid->ssid_len);
  2202. params.filter_ssids[i].ssid_len = ssid->ssid_len;
  2203. params.num_filter_ssids++;
  2204. i++;
  2205. if (i == num_match_ssid)
  2206. break;
  2207. }
  2208. if (ssid->pnext)
  2209. ssid = ssid->pnext;
  2210. else if (prio + 1 == wpa_s->conf->num_prio)
  2211. break;
  2212. else
  2213. ssid = wpa_s->conf->pssid[++prio];
  2214. }
  2215. if (wpa_s->conf->filter_rssi)
  2216. params.filter_rssi = wpa_s->conf->filter_rssi;
  2217. if (wpa_s->sched_scan_plans_num) {
  2218. params.sched_scan_plans = wpa_s->sched_scan_plans;
  2219. params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
  2220. } else {
  2221. /* Set one scan plan that will run infinitely */
  2222. if (wpa_s->conf->sched_scan_interval)
  2223. scan_plan.interval = wpa_s->conf->sched_scan_interval;
  2224. else
  2225. scan_plan.interval = 10;
  2226. scan_plan.iterations = 0;
  2227. params.sched_scan_plans = &scan_plan;
  2228. params.sched_scan_plans_num = 1;
  2229. }
  2230. params.sched_scan_start_delay = wpa_s->conf->sched_scan_start_delay;
  2231. if (params.freqs == NULL && wpa_s->manual_sched_scan_freqs) {
  2232. wpa_dbg(wpa_s, MSG_DEBUG, "Limit sched scan to specified channels");
  2233. params.freqs = wpa_s->manual_sched_scan_freqs;
  2234. }
  2235. if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_PNO) &&
  2236. wpa_s->wpa_state <= WPA_SCANNING) {
  2237. params.mac_addr_rand = 1;
  2238. if (wpa_s->mac_addr_pno) {
  2239. params.mac_addr = wpa_s->mac_addr_pno;
  2240. params.mac_addr_mask = wpa_s->mac_addr_pno + ETH_ALEN;
  2241. }
  2242. }
  2243. wpa_scan_set_relative_rssi_params(wpa_s, &params);
  2244. ret = wpa_supplicant_start_sched_scan(wpa_s, &params);
  2245. os_free(params.filter_ssids);
  2246. if (ret == 0)
  2247. wpa_s->pno = 1;
  2248. else
  2249. wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO");
  2250. return ret;
  2251. }
  2252. int wpas_stop_pno(struct wpa_supplicant *wpa_s)
  2253. {
  2254. int ret = 0;
  2255. if (!wpa_s->pno)
  2256. return 0;
  2257. ret = wpa_supplicant_stop_sched_scan(wpa_s);
  2258. wpa_s->sched_scan_stop_req = 1;
  2259. wpa_s->pno = 0;
  2260. wpa_s->pno_sched_pending = 0;
  2261. if (wpa_s->wpa_state == WPA_SCANNING)
  2262. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2263. return ret;
  2264. }
  2265. void wpas_mac_addr_rand_scan_clear(struct wpa_supplicant *wpa_s,
  2266. unsigned int type)
  2267. {
  2268. type &= MAC_ADDR_RAND_ALL;
  2269. wpa_s->mac_addr_rand_enable &= ~type;
  2270. if (type & MAC_ADDR_RAND_SCAN) {
  2271. os_free(wpa_s->mac_addr_scan);
  2272. wpa_s->mac_addr_scan = NULL;
  2273. }
  2274. if (type & MAC_ADDR_RAND_SCHED_SCAN) {
  2275. os_free(wpa_s->mac_addr_sched_scan);
  2276. wpa_s->mac_addr_sched_scan = NULL;
  2277. }
  2278. if (type & MAC_ADDR_RAND_PNO) {
  2279. os_free(wpa_s->mac_addr_pno);
  2280. wpa_s->mac_addr_pno = NULL;
  2281. }
  2282. }
  2283. int wpas_mac_addr_rand_scan_set(struct wpa_supplicant *wpa_s,
  2284. unsigned int type, const u8 *addr,
  2285. const u8 *mask)
  2286. {
  2287. u8 *tmp = NULL;
  2288. wpas_mac_addr_rand_scan_clear(wpa_s, type);
  2289. if (addr) {
  2290. tmp = os_malloc(2 * ETH_ALEN);
  2291. if (!tmp)
  2292. return -1;
  2293. os_memcpy(tmp, addr, ETH_ALEN);
  2294. os_memcpy(tmp + ETH_ALEN, mask, ETH_ALEN);
  2295. }
  2296. if (type == MAC_ADDR_RAND_SCAN) {
  2297. wpa_s->mac_addr_scan = tmp;
  2298. } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
  2299. wpa_s->mac_addr_sched_scan = tmp;
  2300. } else if (type == MAC_ADDR_RAND_PNO) {
  2301. wpa_s->mac_addr_pno = tmp;
  2302. } else {
  2303. wpa_printf(MSG_INFO,
  2304. "scan: Invalid MAC randomization type=0x%x",
  2305. type);
  2306. os_free(tmp);
  2307. return -1;
  2308. }
  2309. wpa_s->mac_addr_rand_enable |= type;
  2310. return 0;
  2311. }
  2312. int wpas_abort_ongoing_scan(struct wpa_supplicant *wpa_s)
  2313. {
  2314. struct wpa_radio_work *work;
  2315. struct wpa_radio *radio = wpa_s->radio;
  2316. dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) {
  2317. if (work->wpa_s != wpa_s || !work->started ||
  2318. (os_strcmp(work->type, "scan") != 0 &&
  2319. os_strcmp(work->type, "p2p-scan") != 0))
  2320. continue;
  2321. wpa_dbg(wpa_s, MSG_DEBUG, "Abort an ongoing scan");
  2322. return wpa_drv_abort_scan(wpa_s, wpa_s->curr_scan_cookie);
  2323. }
  2324. wpa_dbg(wpa_s, MSG_DEBUG, "No ongoing scan/p2p-scan found to abort");
  2325. return -1;
  2326. }
  2327. int wpas_sched_scan_plans_set(struct wpa_supplicant *wpa_s, const char *cmd)
  2328. {
  2329. struct sched_scan_plan *scan_plans = NULL;
  2330. const char *token, *context = NULL;
  2331. unsigned int num = 0;
  2332. if (!cmd)
  2333. return -1;
  2334. if (!cmd[0]) {
  2335. wpa_printf(MSG_DEBUG, "Clear sched scan plans");
  2336. os_free(wpa_s->sched_scan_plans);
  2337. wpa_s->sched_scan_plans = NULL;
  2338. wpa_s->sched_scan_plans_num = 0;
  2339. return 0;
  2340. }
  2341. while ((token = cstr_token(cmd, " ", &context))) {
  2342. int ret;
  2343. struct sched_scan_plan *scan_plan, *n;
  2344. n = os_realloc_array(scan_plans, num + 1, sizeof(*scan_plans));
  2345. if (!n)
  2346. goto fail;
  2347. scan_plans = n;
  2348. scan_plan = &scan_plans[num];
  2349. num++;
  2350. ret = sscanf(token, "%u:%u", &scan_plan->interval,
  2351. &scan_plan->iterations);
  2352. if (ret <= 0 || ret > 2 || !scan_plan->interval) {
  2353. wpa_printf(MSG_ERROR,
  2354. "Invalid sched scan plan input: %s", token);
  2355. goto fail;
  2356. }
  2357. if (scan_plan->interval > wpa_s->max_sched_scan_plan_interval) {
  2358. wpa_printf(MSG_WARNING,
  2359. "scan plan %u: Scan interval too long(%u), use the maximum allowed(%u)",
  2360. num, scan_plan->interval,
  2361. wpa_s->max_sched_scan_plan_interval);
  2362. scan_plan->interval =
  2363. wpa_s->max_sched_scan_plan_interval;
  2364. }
  2365. if (ret == 1) {
  2366. scan_plan->iterations = 0;
  2367. break;
  2368. }
  2369. if (!scan_plan->iterations) {
  2370. wpa_printf(MSG_ERROR,
  2371. "scan plan %u: Number of iterations cannot be zero",
  2372. num);
  2373. goto fail;
  2374. }
  2375. if (scan_plan->iterations >
  2376. wpa_s->max_sched_scan_plan_iterations) {
  2377. wpa_printf(MSG_WARNING,
  2378. "scan plan %u: Too many iterations(%u), use the maximum allowed(%u)",
  2379. num, scan_plan->iterations,
  2380. wpa_s->max_sched_scan_plan_iterations);
  2381. scan_plan->iterations =
  2382. wpa_s->max_sched_scan_plan_iterations;
  2383. }
  2384. wpa_printf(MSG_DEBUG,
  2385. "scan plan %u: interval=%u iterations=%u",
  2386. num, scan_plan->interval, scan_plan->iterations);
  2387. }
  2388. if (!scan_plans) {
  2389. wpa_printf(MSG_ERROR, "Invalid scan plans entry");
  2390. goto fail;
  2391. }
  2392. if (cstr_token(cmd, " ", &context) || scan_plans[num - 1].iterations) {
  2393. wpa_printf(MSG_ERROR,
  2394. "All scan plans but the last must specify a number of iterations");
  2395. goto fail;
  2396. }
  2397. wpa_printf(MSG_DEBUG, "scan plan %u (last plan): interval=%u",
  2398. num, scan_plans[num - 1].interval);
  2399. if (num > wpa_s->max_sched_scan_plans) {
  2400. wpa_printf(MSG_WARNING,
  2401. "Too many scheduled scan plans (only %u supported)",
  2402. wpa_s->max_sched_scan_plans);
  2403. wpa_printf(MSG_WARNING,
  2404. "Use only the first %u scan plans, and the last one (in infinite loop)",
  2405. wpa_s->max_sched_scan_plans - 1);
  2406. os_memcpy(&scan_plans[wpa_s->max_sched_scan_plans - 1],
  2407. &scan_plans[num - 1], sizeof(*scan_plans));
  2408. num = wpa_s->max_sched_scan_plans;
  2409. }
  2410. os_free(wpa_s->sched_scan_plans);
  2411. wpa_s->sched_scan_plans = scan_plans;
  2412. wpa_s->sched_scan_plans_num = num;
  2413. return 0;
  2414. fail:
  2415. os_free(scan_plans);
  2416. wpa_printf(MSG_ERROR, "invalid scan plans list");
  2417. return -1;
  2418. }
  2419. /**
  2420. * wpas_scan_reset_sched_scan - Reset sched_scan state
  2421. * @wpa_s: Pointer to wpa_supplicant data
  2422. *
  2423. * This function is used to cancel a running scheduled scan and to reset an
  2424. * internal scan state to continue with a regular scan on the following
  2425. * wpa_supplicant_req_scan() calls.
  2426. */
  2427. void wpas_scan_reset_sched_scan(struct wpa_supplicant *wpa_s)
  2428. {
  2429. wpa_s->normal_scans = 0;
  2430. if (wpa_s->sched_scanning) {
  2431. wpa_s->sched_scan_timed_out = 0;
  2432. wpa_s->prev_sched_ssid = NULL;
  2433. wpa_supplicant_cancel_sched_scan(wpa_s);
  2434. }
  2435. }
  2436. void wpas_scan_restart_sched_scan(struct wpa_supplicant *wpa_s)
  2437. {
  2438. /* simulate timeout to restart the sched scan */
  2439. wpa_s->sched_scan_timed_out = 1;
  2440. wpa_s->prev_sched_ssid = NULL;
  2441. wpa_supplicant_cancel_sched_scan(wpa_s);
  2442. }