scan.c 72 KB

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