scan.c 76 KB

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