scan.c 73 KB

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