scan.c 70 KB

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