scan.c 70 KB

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