scan.c 70 KB

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