scan.c 76 KB

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