scan.c 65 KB

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