scan.c 64 KB

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